CAPSTONE PROJECT - CAR DETECTION
COMPUTER VISION PROJECT- PROJECT BY GROUP 8
Milestone 1
Step 1: Import the data
import numpy as np
import plotly.express as px
import os
import zipfile
import cv2
from PIL import Image
import ipywidgets as widgets
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from collections import defaultdict
from io import StringIO
import random
import tensorflow as tf
from sklearn.metrics import accuracy_score
import pandas as pd
import seaborn as sns
from IPython.display import display
import pickle
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications import ResNet50, VGG19, MobileNet, EfficientNetB0, MobileNetV2
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Dropout, Concatenate, Conv2D, Input, BatchNormalization, LeakyReLU, ZeroPadding2D, UpSampling2D
from tensorflow.keras.models import Model, load_model, Sequential
from tensorflow.keras.optimizers import Adam
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, classification_report, confusion_matrix
import tkinter as tk
from tkinter import filedialog
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
import keras.backend as K
import keras.layers as KL
import keras.models as KM
from keras.applications.resnet50 import ResNet50
from keras.optimizers import Adam
from keras.losses import categorical_crossentropy
from keras.callbacks import ModelCheckpoint
%matplotlib inline
car_data_zip_path = 'C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Pre requisites/Car+Images.zip'
annotation_zip_path = 'C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Pre requisites/Annotations.zip'
destination_folder = 'C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Pre requisites'
if not os.path.exists(destination_folder):
os.makedirs(destination_folder)
with zipfile.ZipFile(car_data_zip_path, 'r') as car_data_zip:
car_data_zip.extractall(os.path.join(destination_folder, 'car_data_extracted'))
with zipfile.ZipFile(annotation_zip_path, 'r') as annotation_zip:
annotation_zip.extractall(os.path.join(destination_folder, 'annotations_extracted'))
print("Data extracted and stored in the destination folder.")
Data extracted and stored in the destination folder.
carmodel_file = 'C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Pre requisites/Car+names+and+make.csv'
car_data = pd.read_csv(carmodel_file)
print("Car Data:")
print(car_data.head())
Car Data: AM General Hummer SUV 2000 0 Acura RL Sedan 2012 1 Acura TL Sedan 2012 2 Acura TL Type-S 2008 3 Acura TSX Sedan 2012 4 Acura Integra Type R 2001
Step 2: Map training and testing images to its classes
def process_images(folder_path):
data = []
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
car_name = os.path.basename(root)
car_model = car_name.split()[-1]
car_model_1 = ' '.join(car_name.split()[:-1])
image_name = file
image_location = os.path.join(root, file)
data.append({
'carName': car_name,
'carModel': car_model,
'carModel_1': car_model_1,
'Image Name': image_name,
'Image Location': image_location
})
return data
# Defining the path to the main folder containing subfolders with images
Train_image_folder = 'C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Pre requisites/car_data_extracted/Car Images/Train Images'
# Processing images recursively
Train_data = process_images(Train_image_folder)
# Creating a DataFrame
Train_data_df = pd.DataFrame(Train_data)
# Displaying the final DataFrame with image locations
Train_data_df.head()
| carName | carModel | carModel_1 | Image Name | Image Location | |
|---|---|---|---|---|---|
| 0 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 00198.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... |
| 1 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 00255.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... |
| 2 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 00308.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... |
| 3 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 00374.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... |
| 4 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 00878.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... |
def process_images(folder_path):
data = []
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
car_name = os.path.basename(root)
car_model = car_name.split()[-1]
car_model_1 = ' '.join(car_name.split()[:-1])
image_name = file
image_location = os.path.join(root, file)
data.append({
'carName': car_name,
'carModel': car_model,
'carModel_1': car_model_1,
'Image Name': image_name,
'Image Location': image_location
})
return data
# Defining the path to the main folder containing subfolders with images
Test_image_folder = 'C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Pre requisites/car_data_extracted/Car Images/Test Images'
# Processing images recursively
Test_data = process_images(Test_image_folder)
# Creating DataFrame
Test_data_df = pd.DataFrame(Test_data)
# Displaying the final DataFrame
Test_data_df.head()
| carName | carModel | carModel_1 | Image Name | Image Location | |
|---|---|---|---|---|---|
| 0 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 00128.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... |
| 1 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 00130.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... |
| 2 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 00386.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... |
| 3 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 00565.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... |
| 4 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 00711.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... |
Lets visualize few images
a : Train Images
Train_data_df.shape
(8144, 5)
for index, row in Train_data_df.head(5).iterrows():
image_path = row['Image Location']
img = Image.open(image_path)
plt.imshow(img)
plt.title(f'Image - {row["Image Name"]}\nLocation: {row["Image Location"]}')
plt.show()
b : Test Images
Test_data_df.shape
(8041, 5)
for index, row in Test_data_df.head(5).iterrows():
image_path = row['Image Location']
img = Image.open(image_path)
plt.imshow(img)
plt.title(f'Image - {row["Image Name"]}\nLocation: {row["Image Location"]}')
plt.show()
Step 3: Map training and testing images to its annotations
train_annotations = pd.read_csv("C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Pre requisites/annotations_extracted/Annotations/Train Annotations.csv")
test_annotations = pd.read_csv("C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Pre requisites/annotations_extracted/Annotations/Test Annotation.csv")
train_annotations
| Image Name | Bounding Box coordinates | Unnamed: 2 | Unnamed: 3 | Unnamed: 4 | Image class | |
|---|---|---|---|---|---|---|
| 0 | 00001.jpg | 39 | 116 | 569 | 375 | 14 |
| 1 | 00002.jpg | 36 | 116 | 868 | 587 | 3 |
| 2 | 00003.jpg | 85 | 109 | 601 | 381 | 91 |
| 3 | 00004.jpg | 621 | 393 | 1484 | 1096 | 134 |
| 4 | 00005.jpg | 14 | 36 | 133 | 99 | 106 |
| ... | ... | ... | ... | ... | ... | ... |
| 8139 | 08140.jpg | 3 | 44 | 423 | 336 | 78 |
| 8140 | 08141.jpg | 138 | 150 | 706 | 523 | 196 |
| 8141 | 08142.jpg | 26 | 246 | 660 | 449 | 163 |
| 8142 | 08143.jpg | 78 | 526 | 1489 | 908 | 112 |
| 8143 | 08144.jpg | 20 | 240 | 862 | 677 | 17 |
8144 rows × 6 columns
Lets remane the headings appropriately for the train data!
train_annotations.rename(columns = {'Bounding Box coordinates':'xmin'}, inplace = True)
train_annotations.rename(columns = {'Unnamed: 2':'ymin'}, inplace = True)
train_annotations.rename(columns = {'Unnamed: 3':'xmax'}, inplace = True)
train_annotations.rename(columns = {'Unnamed: 4':'ymax'}, inplace = True)
train_annotations.rename(columns = {'Image class':'Image_class'}, inplace = True)
train_annotations.rename(columns = {'Image Name':'Image Name'}, inplace = True)
train_annotations
| Image Name | xmin | ymin | xmax | ymax | Image_class | |
|---|---|---|---|---|---|---|
| 0 | 00001.jpg | 39 | 116 | 569 | 375 | 14 |
| 1 | 00002.jpg | 36 | 116 | 868 | 587 | 3 |
| 2 | 00003.jpg | 85 | 109 | 601 | 381 | 91 |
| 3 | 00004.jpg | 621 | 393 | 1484 | 1096 | 134 |
| 4 | 00005.jpg | 14 | 36 | 133 | 99 | 106 |
| ... | ... | ... | ... | ... | ... | ... |
| 8139 | 08140.jpg | 3 | 44 | 423 | 336 | 78 |
| 8140 | 08141.jpg | 138 | 150 | 706 | 523 | 196 |
| 8141 | 08142.jpg | 26 | 246 | 660 | 449 | 163 |
| 8142 | 08143.jpg | 78 | 526 | 1489 | 908 | 112 |
| 8143 | 08144.jpg | 20 | 240 | 862 | 677 | 17 |
8144 rows × 6 columns
test_annotations
| Image Name | Bounding Box coordinates | Unnamed: 2 | Unnamed: 3 | Unnamed: 4 | Image class | |
|---|---|---|---|---|---|---|
| 0 | 00001.jpg | 30 | 52 | 246 | 147 | 181 |
| 1 | 00002.jpg | 100 | 19 | 576 | 203 | 103 |
| 2 | 00003.jpg | 51 | 105 | 968 | 659 | 145 |
| 3 | 00004.jpg | 67 | 84 | 581 | 407 | 187 |
| 4 | 00005.jpg | 140 | 151 | 593 | 339 | 185 |
| ... | ... | ... | ... | ... | ... | ... |
| 8036 | 08037.jpg | 49 | 57 | 1169 | 669 | 63 |
| 8037 | 08038.jpg | 23 | 18 | 640 | 459 | 16 |
| 8038 | 08039.jpg | 33 | 27 | 602 | 252 | 17 |
| 8039 | 08040.jpg | 33 | 142 | 521 | 376 | 38 |
| 8040 | 08041.jpg | 77 | 73 | 506 | 380 | 32 |
8041 rows × 6 columns
Lets remane the headings appropriately for the test data!
test_annotations.rename(columns = {'Bounding Box coordinates':'xmin'}, inplace = True)
test_annotations.rename(columns = {'Unnamed: 2':'ymin'}, inplace = True)
test_annotations.rename(columns = {'Unnamed: 3':'xmax'}, inplace = True)
test_annotations.rename(columns = {'Unnamed: 4':'ymax'}, inplace = True)
test_annotations.rename(columns = {'Image class':'Image_class'}, inplace = True)
test_annotations.rename(columns = {'Image Name':'Image Name'}, inplace = True)
test_annotations
| Image Name | xmin | ymin | xmax | ymax | Image_class | |
|---|---|---|---|---|---|---|
| 0 | 00001.jpg | 30 | 52 | 246 | 147 | 181 |
| 1 | 00002.jpg | 100 | 19 | 576 | 203 | 103 |
| 2 | 00003.jpg | 51 | 105 | 968 | 659 | 145 |
| 3 | 00004.jpg | 67 | 84 | 581 | 407 | 187 |
| 4 | 00005.jpg | 140 | 151 | 593 | 339 | 185 |
| ... | ... | ... | ... | ... | ... | ... |
| 8036 | 08037.jpg | 49 | 57 | 1169 | 669 | 63 |
| 8037 | 08038.jpg | 23 | 18 | 640 | 459 | 16 |
| 8038 | 08039.jpg | 33 | 27 | 602 | 252 | 17 |
| 8039 | 08040.jpg | 33 | 142 | 521 | 376 | 38 |
| 8040 | 08041.jpg | 77 | 73 | 506 | 380 | 32 |
8041 rows × 6 columns
Now, Lets merge the dataframe of annotations and image dataframe for the train data!
Train_final_df = pd.merge(Train_data_df, train_annotations, how='inner',left_on='Image Name', right_on='Image Name')
Train_final_df.head(20)
| carName | carModel | carModel_1 | Image Name | Image Location | xmin | ymin | xmax | ymax | Image_class | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 00198.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 91 | 121 | 574 | 357 | 6 |
| 1 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 00255.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 29 | 78 | 734 | 396 | 6 |
| 2 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 00308.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 16 | 136 | 775 | 418 | 6 |
| 3 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 00374.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 9 | 184 | 740 | 499 | 6 |
| 4 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 00878.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 101 | 162 | 882 | 650 | 6 |
| 5 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 00898.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 89 | 68 | 483 | 310 | 6 |
| 6 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 01010.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 6 | 3 | 932 | 639 | 6 |
| 7 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 01012.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 58 | 72 | 763 | 529 | 6 |
| 8 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 01255.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 17 | 80 | 735 | 492 | 6 |
| 9 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 01617.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 62 | 150 | 543 | 339 | 6 |
| 10 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 01864.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 64 | 202 | 898 | 574 | 6 |
| 11 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 01911.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 91 | 175 | 567 | 410 | 6 |
| 12 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 02095.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 90 | 189 | 1495 | 820 | 6 |
| 13 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 02605.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 10 | 34 | 627 | 322 | 6 |
| 14 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 02738.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 86 | 292 | 1176 | 639 | 6 |
| 15 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 02911.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 64 | 240 | 1508 | 943 | 6 |
| 16 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 02919.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 42 | 96 | 612 | 353 | 6 |
| 17 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 03265.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 33 | 150 | 619 | 409 | 6 |
| 18 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 03608.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 180 | 205 | 538 | 400 | 6 |
| 19 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 03670.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 146 | 73 | 486 | 236 | 6 |
train_annotations.iloc[3677]
Image Name 03678.jpg xmin 66 ymin 79 xmax 224 ymax 168 Image_class 190 Name: 3677, dtype: object
Train_final_df.shape
(8144, 10)
Now, Lets merge the dataframne of annotations and image dataframe for the test data!
Test_final_df = pd.merge(Test_data_df, test_annotations,how='inner', left_on='Image Name', right_on='Image Name')
Test_final_df.head(20)
| carName | carModel | carModel_1 | Image Name | Image Location | xmin | ymin | xmax | ymax | Image_class | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 00128.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 122 | 149 | 743 | 455 | 6 |
| 1 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 00130.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 27 | 74 | 774 | 449 | 6 |
| 2 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 00386.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 86 | 184 | 723 | 425 | 6 |
| 3 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 00565.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 15 | 1 | 545 | 347 | 6 |
| 4 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 00711.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 126 | 65 | 735 | 347 | 6 |
| 5 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 01002.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 54 | 225 | 499 | 610 | 6 |
| 6 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 01035.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 25 | 23 | 761 | 529 | 6 |
| 7 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 01176.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 289 | 433 | 1462 | 904 | 6 |
| 8 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 01326.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 40 | 108 | 1250 | 752 | 6 |
| 9 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 01864.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 78 | 110 | 410 | 314 | 6 |
| 10 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 02011.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 30 | 27 | 552 | 306 | 6 |
| 11 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 02182.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 26 | 132 | 1009 | 675 | 6 |
| 12 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 02425.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 37 | 172 | 559 | 412 | 6 |
| 13 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 02433.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 15 | 21 | 632 | 288 | 6 |
| 14 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 02458.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 21 | 218 | 771 | 511 | 6 |
| 15 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 02595.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 55 | 149 | 531 | 359 | 6 |
| 16 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 02742.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 73 | 210 | 439 | 359 | 6 |
| 17 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 02798.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 123 | 82 | 371 | 246 | 6 |
| 18 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 03056.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 99 | 270 | 844 | 583 | 6 |
| 19 | Acura Integra Type R 2001 | 2001 | Acura Integra Type R | 03466.jpg | C:/Users/SAIF/Desktop/Great Learning/Capstone ... | 44 | 147 | 595 | 435 | 6 |
Test_final_df.shape
(8041, 10)
Lets have a look at the sanity of the data and take appropriate steps if necessary!
Train_final_df.info()
<class 'pandas.core.frame.DataFrame'> Int64Index: 8144 entries, 0 to 8143 Data columns (total 10 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 carName 8144 non-null object 1 carModel 8144 non-null object 2 carModel_1 8144 non-null object 3 Image Name 8144 non-null object 4 Image Location 8144 non-null object 5 xmin 8144 non-null int64 6 ymin 8144 non-null int64 7 xmax 8144 non-null int64 8 ymax 8144 non-null int64 9 Image_class 8144 non-null int64 dtypes: int64(5), object(5) memory usage: 699.9+ KB
Test_final_df.info()
<class 'pandas.core.frame.DataFrame'> Int64Index: 8041 entries, 0 to 8040 Data columns (total 10 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 carName 8041 non-null object 1 carModel 8041 non-null object 2 carModel_1 8041 non-null object 3 Image Name 8041 non-null object 4 Image Location 8041 non-null object 5 xmin 8041 non-null int64 6 ymin 8041 non-null int64 7 xmax 8041 non-null int64 8 ymax 8041 non-null int64 9 Image_class 8041 non-null int64 dtypes: int64(5), object(5) memory usage: 691.0+ KB
Train_final_df['carModel'].value_counts().index
Index(['2012', '2007', '2009', '2010', '2011', '2008', '1993', '1994', '1998',
'2001', '2006', '1991', '2000', '2002', '1997', '1999'],
dtype='object')
Test_final_df['carModel'].value_counts().index
Index(['2012', '2007', '2009', '2010', '2011', '2008', '1993', '1994', '1998',
'2001', '1991', '2002', '2006', '2000', '1999', '1997'],
dtype='object')
train_class_percentage = Train_final_df['Image_class'].value_counts(normalize=True) * 100
test_class_percentage = Test_final_df['Image_class'].value_counts(normalize=True) * 100
print("Train Data Class Percentages:")
print(train_class_percentage)
print("\nTest Data Class Percentages:")
print(test_class_percentage)
Train Data Class Percentages:
119 0.834971
79 0.601670
167 0.589391
161 0.589391
144 0.577112
...
175 0.380648
64 0.368369
158 0.356090
99 0.343811
136 0.294695
Name: Image_class, Length: 196, dtype: float64
Test Data Class Percentages:
119 0.845666
161 0.596941
79 0.596941
167 0.584504
43 0.572068
...
175 0.373088
158 0.360652
64 0.360652
99 0.335779
136 0.298470
Name: Image_class, Length: 196, dtype: float64
Lets explore the data by plotting few visualizations and performing EDA.
1 : Test data VS Train Data
total_train_rows = Train_final_df.shape[0]
total_test_rows = Test_final_df.shape[0]
train_percentage = (total_train_rows / (total_train_rows + total_test_rows)) * 100
test_percentage = (total_test_rows / (total_train_rows + total_test_rows)) * 100
sizes = [total_train_rows, total_test_rows]
labels = ['Train Data', 'Test Data']
colors = ['lightcoral', 'lightskyblue']
explode = (0.1, 0)
plt.figure(figsize=(8, 6))
plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', startangle=140)
plt.title('Train-Test Data Percentage Split')
plt.axis('equal')
plt.show()
2 : Cars by year of their origin
carModel_counts = Train_final_df['carModel'].value_counts().reset_index()
carModel_counts.columns = ['carModel', 'Count']
plt.figure(figsize=(10, 6))
plt.bar(carModel_counts['carModel'], carModel_counts['Count'], color='skyblue')
plt.xlabel('Car Model')
plt.ylabel('Count')
plt.title('Train data Cars by year of origin')
plt.xticks(rotation=45, ha='right')
plt.tight_layout()
plt.show()
plt.figure(figsize=(10, 6))
plt.bar(carModel_counts['carModel'], carModel_counts['Count'], color='skyblue')
plt.xlabel('Car Model')
plt.ylabel('Count')
plt.title('Test data Cars by year of origin')
plt.xticks(rotation=45, ha='right')
plt.tight_layout()
plt.show()
3 : Count of top 10
top_10_carModel_1 = Train_final_df['carModel_1'].value_counts().nlargest(10)
bar_colors = ['skyblue', 'orange', 'green', 'red', 'purple', 'pink', 'yellow', 'cyan', 'magenta', 'lightgreen']
plt.figure(figsize=(10, 6))
plt.bar(top_10_carModel_1.index, top_10_carModel_1.values, color=bar_colors)
plt.xlabel('Car Model_1')
plt.ylabel('Count')
plt.title('Top 10 Train Cars')
plt.xticks(rotation=45, ha='right')
plt.tight_layout()
plt.show()
top_10_carModel_1 = Test_final_df['carModel_1'].value_counts().nlargest(10)
bar_colors = ['skyblue', 'orange', 'green', 'red', 'purple', 'pink', 'yellow', 'cyan', 'magenta', 'lightgreen']
plt.figure(figsize=(10, 6))
plt.bar(top_10_carModel_1.index, top_10_carModel_1.values, color=bar_colors)
plt.xlabel('Car Model_1')
plt.ylabel('Count')
plt.title('Top 10 Test Cars')
plt.xticks(rotation=45, ha='right')
plt.tight_layout()
plt.show()
Step 4 : Display images with bounding box
def display_image_with_bounding_box(img_num):
img_path = Train_data_df.loc[img_num, 'Image Location']
img = cv2.imread(img_path)
xmin = int(Train_final_df.loc[img_num, 'xmin'])
ymin = int(Train_final_df.loc[img_num, 'ymin'])
xmax = int(Train_final_df.loc[img_num, 'xmax'])
ymax = int(Train_final_df.loc[img_num, 'ymax'])
cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (0, 255, 0), 2)
car_model = Train_data_df.loc[img_num, 'carModel_1']
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(8, 6))
plt.imshow(img)
plt.title(f'Image with Bounding Box - {Train_data_df.loc[img_num, "Image Name"]}')
plt.axis('off') # Remove axis
plt.show()
print(f'Coordinates: xmin={xmin}, ymin={ymin}, xmax={xmax}, ymax={ymax}')
print(f'Car Model: {car_model}')
random_indices = random.sample(range(len(Train_data_df)), 5)
for img_num in random_indices:
display_image_with_bounding_box(img_num)
Coordinates: xmin=108, ymin=37, xmax=682, ymax=555 Car Model: HUMMER H3T Crew Cab
Coordinates: xmin=12, ymin=58, xmax=591, ymax=302 Car Model: Nissan 240SX Coupe
Coordinates: xmin=7, ymin=164, xmax=636, ymax=342 Car Model: Lincoln Town Car Sedan
Coordinates: xmin=53, ymin=215, xmax=723, ymax=517 Car Model: Audi S4 Sedan
Coordinates: xmin=126, ymin=98, xmax=822, ymax=572 Car Model: HUMMER H2 SUT Crew Cab
Step 5 : Design, train and test basic CNN models to classify the car
!pip install tensorflow
Model 1
img_width, img_height = 224, 224
batch_size = 16
train_datagen = ImageDataGenerator(rescale=1./255,
shear_range=0.1,
zoom_range=0.1,
horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1./255)
train_data_dir = 'C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Pre requisites/car_data_extracted/Car Images/Train Images'
test_data_dir = 'C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Pre requisites/car_data_extracted/Car Images/Test Images'
batch_size = 32
num_classes = len(Train_final_df['Image_class'].unique())
condition = Train_final_df['Image_class'] == 'car'
class_indices = np.where(condition)[0]
class_labels = Train_final_df['Image_class'].iloc[class_indices].tolist()
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='categorical',
classes=class_labels)
validation_classes = sorted(os.listdir(test_data_dir))
validation_generator = test_datagen.flow_from_directory(
test_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='categorical',
classes=validation_classes)
model_1 = Sequential()
model_1.add(Conv2D(16, (3, 3), activation='relu', input_shape=(img_width, img_height, 3)))
model_1.add(MaxPooling2D(pool_size=(2, 2)))
model_1.add(Conv2D(32, (3, 3), activation='relu'))
model_1.add(MaxPooling2D(pool_size=(2, 2)))
model_1.add(Conv2D(128, (3, 3), activation='relu'))
model_1.add(MaxPooling2D(pool_size=(2, 2)))
model_1.add(Flatten())
model_1.add(Dense(64, activation='relu'))
model_1.add(Dense(num_classes, activation='softmax'))
model_1.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
history_1 = model_1.fit(
train_generator,
steps_per_epoch=len(Train_final_df) // batch_size,
epochs=10,
validation_data=validation_generator,
validation_steps=len(Train_final_df) // batch_size)
Found 8144 images belonging to 196 classes. Found 8041 images belonging to 196 classes. Epoch 1/10 254/254 [==============================] - ETA: 0s - loss: 5.2587 - accuracy: 0.0062WARNING:tensorflow:Your input ran out of data; interrupting training. Make sure that your dataset or generator can generate at least `steps_per_epoch * epochs` batches (in this case, 254 batches). You may need to use the repeat() function when building your dataset. 254/254 [==============================] - 291s 1s/step - loss: 5.2587 - accuracy: 0.0062 - val_loss: 5.1854 - val_accuracy: 0.0097 Epoch 2/10 254/254 [==============================] - 203s 800ms/step - loss: 5.1590 - accuracy: 0.0128 Epoch 3/10 254/254 [==============================] - 204s 804ms/step - loss: 5.0984 - accuracy: 0.0144 Epoch 4/10 254/254 [==============================] - 204s 805ms/step - loss: 4.9845 - accuracy: 0.0255 Epoch 5/10 254/254 [==============================] - 209s 824ms/step - loss: 4.8028 - accuracy: 0.0475 Epoch 6/10 254/254 [==============================] - 196s 771ms/step - loss: 4.5783 - accuracy: 0.0690 Epoch 7/10 254/254 [==============================] - 197s 775ms/step - loss: 4.3333 - accuracy: 0.0897 Epoch 8/10 254/254 [==============================] - 196s 770ms/step - loss: 4.0716 - accuracy: 0.1312 Epoch 9/10 254/254 [==============================] - 194s 764ms/step - loss: 3.8233 - accuracy: 0.1658 Epoch 10/10 254/254 [==============================] - 196s 771ms/step - loss: 3.5496 - accuracy: 0.2161
Lets save the Model and Model Weights
model_1.save('C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Milestone 1/Milestone_1_Model_1.h5')
model_1.save_weights('C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Milestone 1/Milestone_1_Model_1_weights.h5')
loaded_model_1 = load_model('C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Milestone 1/Milestone_1_Model_1.h5')
loaded_model_1.load_weights('C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Milestone 1/Milestone_1_Model_1_weights.h5')
Lets analyze the model 1 performance on our Test Data
test_datagen = ImageDataGenerator(rescale=1./255)
test_generator = test_datagen.flow_from_directory(
test_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='categorical',
classes=validation_classes
)
test_loss, test_acc = loaded_model_1.evaluate(test_generator)
print("Test Accuracy:", test_acc)
print("Test Loss:", test_loss)
Found 8041 images belonging to 196 classes. 252/252 [==============================] - 75s 299ms/step - loss: 5.2131 - accuracy: 0.0466 Test Accuracy: 0.04663598909974098 Test Loss: 5.213118076324463
Let plot the Loss as well as Accuracy for Model 1
plt.figure(figsize=(10, 5))
plt.plot(range(1, len(history_1.history['loss']) + 1), history_1.history['loss'], label='Training Loss', marker='o')
plt.title('Model 1 - Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.grid(True)
plt.show()
plt.figure(figsize=(10, 5))
plt.plot(range(1, len(history_1.history['accuracy']) + 1), history_1.history['accuracy'], label='Training Accuracy', marker='o')
plt.title('Model 1 - Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.grid(True)
plt.show()
Model 2
model_2 = Sequential()
model_2.add(Conv2D(32, (3, 3), activation='relu', input_shape=(img_width, img_height, 3)))
model_2.add(MaxPooling2D(pool_size=(2, 2)))
model_2.add(Conv2D(64, (3, 3), activation='sigmoid'))
model_2.add(MaxPooling2D(pool_size=(2, 2)))
# Number of Conventional Layers reduced
model_2.add(Flatten())
model_2.add(Dense(64, activation='sigmoid'))
model_2.add(Dense(num_classes, activation='sigmoid')) # Activation Function for output layer changed
model_2.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])
# Optimizer changed from adam to Standard-Gradient-Descent(SGD)
history_2 = model_2.fit(
train_generator,
steps_per_epoch=len(Train_final_df) // batch_size,
epochs=5, # Reduced epochs for faster training of basic model
validation_data=validation_generator,
validation_steps=len(Train_final_df) // batch_size)
Epoch 1/5 254/254 [==============================] - ETA: 0s - loss: 5.2871 - accuracy: 0.0053WARNING:tensorflow:Your input ran out of data; interrupting training. Make sure that your dataset or generator can generate at least `steps_per_epoch * epochs` batches (in this case, 254 batches). You may need to use the repeat() function when building your dataset. 254/254 [==============================] - 393s 2s/step - loss: 5.2871 - accuracy: 0.0053 - val_loss: 5.2779 - val_accuracy: 0.0055 Epoch 2/5 254/254 [==============================] - 275s 1s/step - loss: 5.2787 - accuracy: 0.0063 Epoch 3/5 254/254 [==============================] - 274s 1s/step - loss: 5.2787 - accuracy: 0.0065 Epoch 4/5 254/254 [==============================] - 277s 1s/step - loss: 5.2779 - accuracy: 0.0083 Epoch 5/5 254/254 [==============================] - 274s 1s/step - loss: 5.2779 - accuracy: 0.0083
Lets save the Model and Model Weights
model_2.save('C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Milestone 1/Milestone_1_Model_2.h5')
model_2.save_weights('C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Milestone 1/Milestone_1_Model_2_weights.h5')
loaded_model_2 = load_model('C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Milestone 1/Milestone_1_Model_2.h5')
loaded_model_2.load_weights('C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Milestone 1/Milestone_1_Model_2_weights.h5')
Lets analyze the model 2 performance on our Test Data
test_datagen = ImageDataGenerator(rescale=1./255)
test_generator = test_datagen.flow_from_directory(
test_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='categorical',
classes=validation_classes
)
test_loss, test_acc = loaded_model_2.evaluate(test_generator)
print("Test Accuracy:", test_acc)
print("Test Loss:", test_loss)
Found 8041 images belonging to 196 classes. 252/252 [==============================] - 95s 375ms/step - loss: 5.2773 - accuracy: 0.0085 Test Accuracy: 0.008456659503281116 Test Loss: 5.277335166931152
Let plot the Loss as well as Accuracy for Model 2
plt.figure(figsize=(10, 5))
plt.plot(range(1, len(history_2.history['loss']) + 1), history_2.history['loss'], label='Training Loss', marker='o')
plt.title('Model 1 - Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.grid(True)
plt.show()
plt.figure(figsize=(10, 5))
plt.plot(range(1, len(history_2.history['accuracy']) + 1), history_2.history['accuracy'], label='Training Accuracy', marker='o')
plt.title('Model 1 - Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.grid(True)
plt.show()
Model 3
model_3 = Sequential()
model_3.add(Conv2D(32, (3, 3), activation='relu', input_shape=(img_width, img_height, 3)))
model_3.add(MaxPooling2D(pool_size=(2, 2)))
model_3.add(Conv2D(64, (3, 3), activation='relu'))
model_3.add(MaxPooling2D(pool_size=(2, 2)))
model_3.add(Conv2D(128, (3, 3), activation='relu'))
model_3.add(MaxPooling2D(pool_size=(2, 2)))
model_3.add(Conv2D(256, (3, 3), activation='relu'))
model_3.add(MaxPooling2D(pool_size=(2, 2)))
model_3.add(Conv2D(512, (3, 3), activation='relu'))
model_3.add(MaxPooling2D(pool_size=(2, 2)))
model_3.add(Flatten())
model_3.add(Dense(512, activation='relu'))
model_3.add(Dropout(0.5))
model_3.add(Dense(num_classes, activation='softmax'))
model_3.compile(loss='categorical_crossentropy', optimizer='adagrad', metrics=['accuracy'])
early_stopping = EarlyStopping(monitor='val_loss', patience=3, restore_best_weights=True)
history_3 = model_3.fit(
train_generator,
steps_per_epoch=len(train_generator),
epochs=10,
validation_data=validation_generator,
validation_steps=len(validation_generator),
callbacks=[early_stopping])
Epoch 1/10 255/255 [==============================] - 435s 2s/step - loss: 5.2806 - accuracy: 0.0047 - val_loss: 5.2779 - val_accuracy: 0.0057 Epoch 2/10 255/255 [==============================] - 433s 2s/step - loss: 5.2789 - accuracy: 0.0048 - val_loss: 5.2768 - val_accuracy: 0.0073 Epoch 3/10 255/255 [==============================] - 434s 2s/step - loss: 5.2772 - accuracy: 0.0070 - val_loss: 5.2763 - val_accuracy: 0.0056 Epoch 4/10 255/255 [==============================] - 430s 2s/step - loss: 5.2775 - accuracy: 0.0048 - val_loss: 5.2759 - val_accuracy: 0.0065 Epoch 5/10 255/255 [==============================] - 434s 2s/step - loss: 5.2766 - accuracy: 0.0056 - val_loss: 5.2756 - val_accuracy: 0.0075 Epoch 6/10 255/255 [==============================] - 432s 2s/step - loss: 5.2767 - accuracy: 0.0070 - val_loss: 5.2753 - val_accuracy: 0.0073 Epoch 7/10 255/255 [==============================] - 432s 2s/step - loss: 5.2757 - accuracy: 0.0061 - val_loss: 5.2751 - val_accuracy: 0.0075 Epoch 8/10 255/255 [==============================] - 430s 2s/step - loss: 5.2754 - accuracy: 0.0063 - val_loss: 5.2747 - val_accuracy: 0.0078 Epoch 9/10 255/255 [==============================] - 433s 2s/step - loss: 5.2761 - accuracy: 0.0076 - val_loss: 5.2745 - val_accuracy: 0.0085 Epoch 10/10 255/255 [==============================] - 432s 2s/step - loss: 5.2752 - accuracy: 0.0060 - val_loss: 5.2742 - val_accuracy: 0.0085
Lets save the Model and Model Weights
model_3.save('C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Milestone 1/Milestone_1_Model_3.h5')
model_3.save_weights('C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Milestone 1/Milestone_1_Model_3_weights.h5')
loaded_model_3 = load_model('C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Milestone 1/Milestone_1_Model_3.h5')
loaded_model_3.load_weights('C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Milestone 1/Milestone_1_Model_3_weights.h5')
Lets analyze the model 3 performance on our Test Data
test_datagen = ImageDataGenerator(rescale=1./255)
test_generator = test_datagen.flow_from_directory(
test_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='categorical',
classes=validation_classes
)
test_loss, test_acc = loaded_model_3.evaluate(test_generator)
print("Test Accuracy:", test_acc)
print("Test Loss:", test_loss)
Found 8041 images belonging to 196 classes. 252/252 [==============================] - 116s 458ms/step - loss: 5.2742 - accuracy: 0.0085 Test Accuracy: 0.008456659503281116 Test Loss: 5.274209499359131
Let plot the Training and Validation Loss as well as Training and Validation Accuracy for Model 3
plt.figure(figsize=(10, 5))
plt.plot(range(1, len(history_3.history['loss']) + 1), history_3.history['loss'], label='Training Loss', marker='o')
plt.title('Model 1 - Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.grid(True)
plt.show()
plt.figure(figsize=(10, 5))
plt.plot(range(1, len(history_3.history['accuracy']) + 1), history_3.history['accuracy'], label='Training Accuracy', marker='o')
plt.title('Model 1 - Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.grid(True)
plt.show()
Lets compare and evaluate the performace of all the 3 CNN models
train_acc_1 = history_1.history['accuracy']
test_acc_1 = history_1.history['val_accuracy']
train_loss_1 = history_1.history['loss']
test_loss_1 = history_1.history['val_loss']
train_acc_2 = history_2.history['accuracy']
test_acc_2 = history_2.history['val_accuracy']
train_loss_2 = history_2.history['loss']
test_loss_2 = history_2.history['val_loss']
train_acc_3 = history_3.history['accuracy']
test_acc_3 = history_3.history['val_accuracy']
train_loss_3 = history_3.history['loss']
test_loss_3 = history_3.history['val_loss']
final_train_acc_1 = train_acc_1[-1]
final_test_acc_1 = test_acc_1[-1]
final_train_loss_1 = train_loss_1[-1]
final_test_loss_1 = test_loss_1[-1]
final_train_acc_2 = train_acc_2[-1]
final_test_acc_2 = test_acc_2[-1]
final_train_loss_2 = train_loss_2[-1]
final_test_loss_2 = test_loss_2[-1]
final_train_acc_3 = train_acc_3[-1]
final_test_acc_3 = test_acc_3[-1]
final_train_loss_3 = train_loss_3[-1]
final_test_loss_3 = test_loss_3[-1]
metrics_data = {
'Model': ['Model 1', 'Model 2', 'Model 3'],
'Train Accuracy': [final_train_acc_1, final_train_acc_2, final_train_acc_3],
'Test Accuracy': [final_test_acc_1, final_test_acc_2, final_test_acc_3],
'Train Loss': [final_train_loss_1, final_train_loss_2, final_train_loss_3],
'Test Loss': [final_test_loss_1, final_test_loss_2, final_test_loss_3]
}
metrics_df = pd.DataFrame(metrics_data)
metrics_df
| Model | Train Accuracy | Test Accuracy | Train Loss | Test Loss | |
|---|---|---|---|---|---|
| 0 | Model 1 | 0.216100 | 0.009700 | 3.549627 | 5.185400 |
| 1 | Model 2 | 0.008259 | 0.005472 | 5.277876 | 5.277879 |
| 2 | Model 3 | 0.006017 | 0.008457 | 5.275213 | 5.274206 |
Insights and Observations :
Model 1:
- Train Accuracy: 21.61%
- Test Accuracy: 0.97%
- Train Loss: 3.55
- Test Loss: 5.19
- This model demonstrates a notably higher train accuracy compared to the test accuracy, indicating potential overfitting. The high discrepancy between the train and test accuracies suggests that the model may not generalize well to unseen data.
Model 2:
- Train Accuracy: 0.83%
- Test Accuracy: 0.55%
- Train Loss: 5.28
- Test Loss: 5.28
- Both the train and test accuracies are quite low, suggesting that this model is not capturing the patterns in the data effectively. The similar train and test losses indicate that the model is not overfitting, but it is not learning the underlying patterns well.
Model 3:
- Train Accuracy: 0.60%
- Test Accuracy: 0.85%
- Train Loss: 5.28
- Test Loss: 5.27
- Similar to Model 2, this model also shows low train and test accuracies. However, the test accuracy is comparatively higher than the train accuracy, indicating that it might be performing slightly better on unseen data.
Overall Assessment:
- All three models exhibit low test accuracies, suggesting that they are not effectively capturing the patterns in the data.
- Model 1 shows signs of overfitting, while Models 2 and 3 display poor performance on both the train and test sets.
Methods of Optimization:
- The activation function (sigmoid) and optimizer (SGD) selected by Model 2 for the output layer are different from those of the other models (softmax, Adam and Adagrad).
- The variations in model performance that have been reported may be attributed to these changes in optimization strategies.
Let's now, at last, see how well each of our three models has done in terms of classifying the car photos.
Model 1
num_images_to_test = 5
current_index = 0
while current_index < len(test_images):
actual_label = test_labels[current_index].argmax()
predictions = loaded_model_1.predict(test_images[current_index].reshape(1, img_width, img_height, 3))
predicted_label = predictions[0].argmax()
plt.imshow(test_images[current_index])
plt.title(f'Actual: {actual_label}, Predicted: {predicted_label}')
plt.show()
choice = input("Press 'n' to see the next image, or 'q' to quit: ")
if choice == 'n':
current_index += 1
elif choice == 'q':
break
else:
print("Invalid choice. Please try again.")
print("End of interactive image display.")
1/1 [==============================] - 0s 86ms/step
Press 'n' to see the next image, or 'q' to quit: n 1/1 [==============================] - 0s 56ms/step
Press 'n' to see the next image, or 'q' to quit: n 1/1 [==============================] - 0s 35ms/step
Press 'n' to see the next image, or 'q' to quit: n 1/1 [==============================] - 0s 36ms/step
Press 'n' to see the next image, or 'q' to quit: n 1/1 [==============================] - 0s 35ms/step
Press 'n' to see the next image, or 'q' to quit: n 1/1 [==============================] - 0s 35ms/step
Press 'n' to see the next image, or 'q' to quit: q End of interactive image display.
Model 2
num_images_to_test = 5
current_index = 0
while current_index < len(test_images):
actual_label = test_labels[current_index].argmax()
predictions = loaded_model_2.predict(test_images[current_index].reshape(1, img_width, img_height, 3))
predicted_label = predictions[0].argmax()
plt.imshow(test_images[current_index])
plt.title(f'Actual: {actual_label}, Predicted: {predicted_label}')
plt.show()
choice = input("Press 'n' to see the next image, or 'q' to quit: ")
if choice == 'n':
current_index += 1
elif choice == 'q':
break
else:
print("Invalid choice. Please try again.")
print("End of interactive image display.")
1/1 [==============================] - 0s 40ms/step
Press 'n' to see the next image, or 'q' to quit: n 1/1 [==============================] - 0s 45ms/step
Press 'n' to see the next image, or 'q' to quit: n 1/1 [==============================] - 0s 42ms/step
Press 'n' to see the next image, or 'q' to quit: n 1/1 [==============================] - 0s 44ms/step
Press 'n' to see the next image, or 'q' to quit: n 1/1 [==============================] - 0s 46ms/step
Press 'n' to see the next image, or 'q' to quit: n 1/1 [==============================] - 0s 36ms/step
Press 'n' to see the next image, or 'q' to quit: q End of interactive image display.
Model 3
num_images_to_test = 5
current_index = 0
while current_index < len(test_images):
actual_label = test_labels[current_index].argmax()
predictions = loaded_model_3.predict(test_images[current_index].reshape(1, img_width, img_height, 3))
predicted_label = predictions[0].argmax()
plt.imshow(test_images[current_index])
plt.title(f'Actual: {actual_label}, Predicted: {predicted_label}')
plt.show()
choice = input("Press 'n' to see the next image, or 'q' to quit: ")
if choice == 'n':
current_index += 1
elif choice == 'q':
break
else:
print("Invalid choice. Please try again.")
print("End of interactive image display.")
1/1 [==============================] - 0s 27ms/step
Press 'n' to see the next image, or 'q' to quit: n 1/1 [==============================] - 0s 38ms/step
Press 'n' to see the next image, or 'q' to quit: n 1/1 [==============================] - 0s 27ms/step
Press 'n' to see the next image, or 'q' to quit: n 1/1 [==============================] - 0s 27ms/step
Press 'n' to see the next image, or 'q' to quit: n 1/1 [==============================] - 0s 28ms/step
Press 'n' to see the next image, or 'q' to quit: n 1/1 [==============================] - 0s 29ms/step
Press 'n' to see the next image, or 'q' to quit: q End of interactive image display.
Milestone 2
Step 1: Fine tune the trained basic CNN models to classify the car.
Lets create a copy of our original Train Dataset Train_final_df
Model 1 : With Transfer Learning Model : ResNet_50
# Creating a copy of the DataFrame
Train_final_df_copy = Train_final_df.copy()
# Preprocessing class labels using label encoder
label_encoder = LabelEncoder()
Train_final_df_copy['Encoded_Labels'] = label_encoder.fit_transform(Train_final_df['Image_class'])
num_classes = len(label_encoder.classes_)
# Splitting data into training and validation sets
train_data_new, val_data = train_test_split(Train_final_df_copy, test_size=0.2, random_state=42)
# Defining image parameters
IMAGE_SIZE = (224, 224)
BATCH_SIZE = 64
# Performing data augmentation for training images
train_datagen = ImageDataGenerator(rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
# Preprocessing images for validation and test
val_test_datagen = ImageDataGenerator(rescale=1./255)
# Converting integer labels back to class names and converting them to strings
train_data_new['Class_Labels'] = label_encoder.inverse_transform(train_data_new['Encoded_Labels']).astype(str)
val_data['Class_Labels'] = label_encoder.inverse_transform(val_data['Encoded_Labels']).astype(str)
# Defining data generators for training and validation
train_generator = train_datagen.flow_from_dataframe(dataframe=train_data_new,
x_col="Image Location",
y_col="Class_Labels", # Using class names as y_col
target_size=IMAGE_SIZE,
batch_size=BATCH_SIZE,
class_mode='categorical', # Setting class_mode to 'categorical'
shuffle=True) # Shuffling the data
val_generator = val_test_datagen.flow_from_dataframe(dataframe=val_data,
x_col="Image Location",
y_col="Class_Labels", # Using class names as y_col
target_size=IMAGE_SIZE,
batch_size=BATCH_SIZE,
class_mode='categorical', # Setting class_mode to 'categorical'
shuffle=False) # Not shuffling validation data
# Loading pre-trained ResNet50 model
base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
x = Dense(512, activation='relu')(x) # Adding an additional dense layer
x = Dense(256, activation='relu')(x) # Adding another dense layer
predictions = Dense(num_classes, activation='softmax')(x) # Output layer, adjusted to num_classes
# Creating the transfer learning model
model_reset50 = Model(inputs=base_model.input, outputs=predictions)
# Freezing the first 50 layers of the pre-trained model
for layer in model_reset50.layers[:51]:
layer.trainable = False
# Training the remaining layers
for layer in model_reset50.layers[51:]:
layer.trainable = True
# Compiling the model
model_reset50.compile(optimizer=Adam(), loss='categorical_crossentropy', metrics=['accuracy'])
# Printing model summary
model_reset50.summary()
# Defining checkpoint to save the best model
checkpoint = ModelCheckpoint('best_model.h5', monitor='val_accuracy', save_best_only=True, mode='max', verbose=1)
Found 6515 validated image filenames belonging to 196 classes.
Found 1629 validated image filenames belonging to 196 classes.
Model: "model_4"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_9 (InputLayer) [(None, 224, 224, 3)] 0 []
conv1_pad (ZeroPadding2D) (None, 230, 230, 3) 0 ['input_9[0][0]']
conv1_conv (Conv2D) (None, 112, 112, 64) 9472 ['conv1_pad[0][0]']
conv1_bn (BatchNormalizati (None, 112, 112, 64) 256 ['conv1_conv[0][0]']
on)
conv1_relu (Activation) (None, 112, 112, 64) 0 ['conv1_bn[0][0]']
pool1_pad (ZeroPadding2D) (None, 114, 114, 64) 0 ['conv1_relu[0][0]']
pool1_pool (MaxPooling2D) (None, 56, 56, 64) 0 ['pool1_pad[0][0]']
conv2_block1_1_conv (Conv2 (None, 56, 56, 64) 4160 ['pool1_pool[0][0]']
D)
conv2_block1_1_bn (BatchNo (None, 56, 56, 64) 256 ['conv2_block1_1_conv[0][0]']
rmalization)
conv2_block1_1_relu (Activ (None, 56, 56, 64) 0 ['conv2_block1_1_bn[0][0]']
ation)
conv2_block1_2_conv (Conv2 (None, 56, 56, 64) 36928 ['conv2_block1_1_relu[0][0]']
D)
conv2_block1_2_bn (BatchNo (None, 56, 56, 64) 256 ['conv2_block1_2_conv[0][0]']
rmalization)
conv2_block1_2_relu (Activ (None, 56, 56, 64) 0 ['conv2_block1_2_bn[0][0]']
ation)
conv2_block1_0_conv (Conv2 (None, 56, 56, 256) 16640 ['pool1_pool[0][0]']
D)
conv2_block1_3_conv (Conv2 (None, 56, 56, 256) 16640 ['conv2_block1_2_relu[0][0]']
D)
conv2_block1_0_bn (BatchNo (None, 56, 56, 256) 1024 ['conv2_block1_0_conv[0][0]']
rmalization)
conv2_block1_3_bn (BatchNo (None, 56, 56, 256) 1024 ['conv2_block1_3_conv[0][0]']
rmalization)
conv2_block1_add (Add) (None, 56, 56, 256) 0 ['conv2_block1_0_bn[0][0]',
'conv2_block1_3_bn[0][0]']
conv2_block1_out (Activati (None, 56, 56, 256) 0 ['conv2_block1_add[0][0]']
on)
conv2_block2_1_conv (Conv2 (None, 56, 56, 64) 16448 ['conv2_block1_out[0][0]']
D)
conv2_block2_1_bn (BatchNo (None, 56, 56, 64) 256 ['conv2_block2_1_conv[0][0]']
rmalization)
conv2_block2_1_relu (Activ (None, 56, 56, 64) 0 ['conv2_block2_1_bn[0][0]']
ation)
conv2_block2_2_conv (Conv2 (None, 56, 56, 64) 36928 ['conv2_block2_1_relu[0][0]']
D)
conv2_block2_2_bn (BatchNo (None, 56, 56, 64) 256 ['conv2_block2_2_conv[0][0]']
rmalization)
conv2_block2_2_relu (Activ (None, 56, 56, 64) 0 ['conv2_block2_2_bn[0][0]']
ation)
conv2_block2_3_conv (Conv2 (None, 56, 56, 256) 16640 ['conv2_block2_2_relu[0][0]']
D)
conv2_block2_3_bn (BatchNo (None, 56, 56, 256) 1024 ['conv2_block2_3_conv[0][0]']
rmalization)
conv2_block2_add (Add) (None, 56, 56, 256) 0 ['conv2_block1_out[0][0]',
'conv2_block2_3_bn[0][0]']
conv2_block2_out (Activati (None, 56, 56, 256) 0 ['conv2_block2_add[0][0]']
on)
conv2_block3_1_conv (Conv2 (None, 56, 56, 64) 16448 ['conv2_block2_out[0][0]']
D)
conv2_block3_1_bn (BatchNo (None, 56, 56, 64) 256 ['conv2_block3_1_conv[0][0]']
rmalization)
conv2_block3_1_relu (Activ (None, 56, 56, 64) 0 ['conv2_block3_1_bn[0][0]']
ation)
conv2_block3_2_conv (Conv2 (None, 56, 56, 64) 36928 ['conv2_block3_1_relu[0][0]']
D)
conv2_block3_2_bn (BatchNo (None, 56, 56, 64) 256 ['conv2_block3_2_conv[0][0]']
rmalization)
conv2_block3_2_relu (Activ (None, 56, 56, 64) 0 ['conv2_block3_2_bn[0][0]']
ation)
conv2_block3_3_conv (Conv2 (None, 56, 56, 256) 16640 ['conv2_block3_2_relu[0][0]']
D)
conv2_block3_3_bn (BatchNo (None, 56, 56, 256) 1024 ['conv2_block3_3_conv[0][0]']
rmalization)
conv2_block3_add (Add) (None, 56, 56, 256) 0 ['conv2_block2_out[0][0]',
'conv2_block3_3_bn[0][0]']
conv2_block3_out (Activati (None, 56, 56, 256) 0 ['conv2_block3_add[0][0]']
on)
conv3_block1_1_conv (Conv2 (None, 28, 28, 128) 32896 ['conv2_block3_out[0][0]']
D)
conv3_block1_1_bn (BatchNo (None, 28, 28, 128) 512 ['conv3_block1_1_conv[0][0]']
rmalization)
conv3_block1_1_relu (Activ (None, 28, 28, 128) 0 ['conv3_block1_1_bn[0][0]']
ation)
conv3_block1_2_conv (Conv2 (None, 28, 28, 128) 147584 ['conv3_block1_1_relu[0][0]']
D)
conv3_block1_2_bn (BatchNo (None, 28, 28, 128) 512 ['conv3_block1_2_conv[0][0]']
rmalization)
conv3_block1_2_relu (Activ (None, 28, 28, 128) 0 ['conv3_block1_2_bn[0][0]']
ation)
conv3_block1_0_conv (Conv2 (None, 28, 28, 512) 131584 ['conv2_block3_out[0][0]']
D)
conv3_block1_3_conv (Conv2 (None, 28, 28, 512) 66048 ['conv3_block1_2_relu[0][0]']
D)
conv3_block1_0_bn (BatchNo (None, 28, 28, 512) 2048 ['conv3_block1_0_conv[0][0]']
rmalization)
conv3_block1_3_bn (BatchNo (None, 28, 28, 512) 2048 ['conv3_block1_3_conv[0][0]']
rmalization)
conv3_block1_add (Add) (None, 28, 28, 512) 0 ['conv3_block1_0_bn[0][0]',
'conv3_block1_3_bn[0][0]']
conv3_block1_out (Activati (None, 28, 28, 512) 0 ['conv3_block1_add[0][0]']
on)
conv3_block2_1_conv (Conv2 (None, 28, 28, 128) 65664 ['conv3_block1_out[0][0]']
D)
conv3_block2_1_bn (BatchNo (None, 28, 28, 128) 512 ['conv3_block2_1_conv[0][0]']
rmalization)
conv3_block2_1_relu (Activ (None, 28, 28, 128) 0 ['conv3_block2_1_bn[0][0]']
ation)
conv3_block2_2_conv (Conv2 (None, 28, 28, 128) 147584 ['conv3_block2_1_relu[0][0]']
D)
conv3_block2_2_bn (BatchNo (None, 28, 28, 128) 512 ['conv3_block2_2_conv[0][0]']
rmalization)
conv3_block2_2_relu (Activ (None, 28, 28, 128) 0 ['conv3_block2_2_bn[0][0]']
ation)
conv3_block2_3_conv (Conv2 (None, 28, 28, 512) 66048 ['conv3_block2_2_relu[0][0]']
D)
conv3_block2_3_bn (BatchNo (None, 28, 28, 512) 2048 ['conv3_block2_3_conv[0][0]']
rmalization)
conv3_block2_add (Add) (None, 28, 28, 512) 0 ['conv3_block1_out[0][0]',
'conv3_block2_3_bn[0][0]']
conv3_block2_out (Activati (None, 28, 28, 512) 0 ['conv3_block2_add[0][0]']
on)
conv3_block3_1_conv (Conv2 (None, 28, 28, 128) 65664 ['conv3_block2_out[0][0]']
D)
conv3_block3_1_bn (BatchNo (None, 28, 28, 128) 512 ['conv3_block3_1_conv[0][0]']
rmalization)
conv3_block3_1_relu (Activ (None, 28, 28, 128) 0 ['conv3_block3_1_bn[0][0]']
ation)
conv3_block3_2_conv (Conv2 (None, 28, 28, 128) 147584 ['conv3_block3_1_relu[0][0]']
D)
conv3_block3_2_bn (BatchNo (None, 28, 28, 128) 512 ['conv3_block3_2_conv[0][0]']
rmalization)
conv3_block3_2_relu (Activ (None, 28, 28, 128) 0 ['conv3_block3_2_bn[0][0]']
ation)
conv3_block3_3_conv (Conv2 (None, 28, 28, 512) 66048 ['conv3_block3_2_relu[0][0]']
D)
conv3_block3_3_bn (BatchNo (None, 28, 28, 512) 2048 ['conv3_block3_3_conv[0][0]']
rmalization)
conv3_block3_add (Add) (None, 28, 28, 512) 0 ['conv3_block2_out[0][0]',
'conv3_block3_3_bn[0][0]']
conv3_block3_out (Activati (None, 28, 28, 512) 0 ['conv3_block3_add[0][0]']
on)
conv3_block4_1_conv (Conv2 (None, 28, 28, 128) 65664 ['conv3_block3_out[0][0]']
D)
conv3_block4_1_bn (BatchNo (None, 28, 28, 128) 512 ['conv3_block4_1_conv[0][0]']
rmalization)
conv3_block4_1_relu (Activ (None, 28, 28, 128) 0 ['conv3_block4_1_bn[0][0]']
ation)
conv3_block4_2_conv (Conv2 (None, 28, 28, 128) 147584 ['conv3_block4_1_relu[0][0]']
D)
conv3_block4_2_bn (BatchNo (None, 28, 28, 128) 512 ['conv3_block4_2_conv[0][0]']
rmalization)
conv3_block4_2_relu (Activ (None, 28, 28, 128) 0 ['conv3_block4_2_bn[0][0]']
ation)
conv3_block4_3_conv (Conv2 (None, 28, 28, 512) 66048 ['conv3_block4_2_relu[0][0]']
D)
conv3_block4_3_bn (BatchNo (None, 28, 28, 512) 2048 ['conv3_block4_3_conv[0][0]']
rmalization)
conv3_block4_add (Add) (None, 28, 28, 512) 0 ['conv3_block3_out[0][0]',
'conv3_block4_3_bn[0][0]']
conv3_block4_out (Activati (None, 28, 28, 512) 0 ['conv3_block4_add[0][0]']
on)
conv4_block1_1_conv (Conv2 (None, 14, 14, 256) 131328 ['conv3_block4_out[0][0]']
D)
conv4_block1_1_bn (BatchNo (None, 14, 14, 256) 1024 ['conv4_block1_1_conv[0][0]']
rmalization)
conv4_block1_1_relu (Activ (None, 14, 14, 256) 0 ['conv4_block1_1_bn[0][0]']
ation)
conv4_block1_2_conv (Conv2 (None, 14, 14, 256) 590080 ['conv4_block1_1_relu[0][0]']
D)
conv4_block1_2_bn (BatchNo (None, 14, 14, 256) 1024 ['conv4_block1_2_conv[0][0]']
rmalization)
conv4_block1_2_relu (Activ (None, 14, 14, 256) 0 ['conv4_block1_2_bn[0][0]']
ation)
conv4_block1_0_conv (Conv2 (None, 14, 14, 1024) 525312 ['conv3_block4_out[0][0]']
D)
conv4_block1_3_conv (Conv2 (None, 14, 14, 1024) 263168 ['conv4_block1_2_relu[0][0]']
D)
conv4_block1_0_bn (BatchNo (None, 14, 14, 1024) 4096 ['conv4_block1_0_conv[0][0]']
rmalization)
conv4_block1_3_bn (BatchNo (None, 14, 14, 1024) 4096 ['conv4_block1_3_conv[0][0]']
rmalization)
conv4_block1_add (Add) (None, 14, 14, 1024) 0 ['conv4_block1_0_bn[0][0]',
'conv4_block1_3_bn[0][0]']
conv4_block1_out (Activati (None, 14, 14, 1024) 0 ['conv4_block1_add[0][0]']
on)
conv4_block2_1_conv (Conv2 (None, 14, 14, 256) 262400 ['conv4_block1_out[0][0]']
D)
conv4_block2_1_bn (BatchNo (None, 14, 14, 256) 1024 ['conv4_block2_1_conv[0][0]']
rmalization)
conv4_block2_1_relu (Activ (None, 14, 14, 256) 0 ['conv4_block2_1_bn[0][0]']
ation)
conv4_block2_2_conv (Conv2 (None, 14, 14, 256) 590080 ['conv4_block2_1_relu[0][0]']
D)
conv4_block2_2_bn (BatchNo (None, 14, 14, 256) 1024 ['conv4_block2_2_conv[0][0]']
rmalization)
conv4_block2_2_relu (Activ (None, 14, 14, 256) 0 ['conv4_block2_2_bn[0][0]']
ation)
conv4_block2_3_conv (Conv2 (None, 14, 14, 1024) 263168 ['conv4_block2_2_relu[0][0]']
D)
conv4_block2_3_bn (BatchNo (None, 14, 14, 1024) 4096 ['conv4_block2_3_conv[0][0]']
rmalization)
conv4_block2_add (Add) (None, 14, 14, 1024) 0 ['conv4_block1_out[0][0]',
'conv4_block2_3_bn[0][0]']
conv4_block2_out (Activati (None, 14, 14, 1024) 0 ['conv4_block2_add[0][0]']
on)
conv4_block3_1_conv (Conv2 (None, 14, 14, 256) 262400 ['conv4_block2_out[0][0]']
D)
conv4_block3_1_bn (BatchNo (None, 14, 14, 256) 1024 ['conv4_block3_1_conv[0][0]']
rmalization)
conv4_block3_1_relu (Activ (None, 14, 14, 256) 0 ['conv4_block3_1_bn[0][0]']
ation)
conv4_block3_2_conv (Conv2 (None, 14, 14, 256) 590080 ['conv4_block3_1_relu[0][0]']
D)
conv4_block3_2_bn (BatchNo (None, 14, 14, 256) 1024 ['conv4_block3_2_conv[0][0]']
rmalization)
conv4_block3_2_relu (Activ (None, 14, 14, 256) 0 ['conv4_block3_2_bn[0][0]']
ation)
conv4_block3_3_conv (Conv2 (None, 14, 14, 1024) 263168 ['conv4_block3_2_relu[0][0]']
D)
conv4_block3_3_bn (BatchNo (None, 14, 14, 1024) 4096 ['conv4_block3_3_conv[0][0]']
rmalization)
conv4_block3_add (Add) (None, 14, 14, 1024) 0 ['conv4_block2_out[0][0]',
'conv4_block3_3_bn[0][0]']
conv4_block3_out (Activati (None, 14, 14, 1024) 0 ['conv4_block3_add[0][0]']
on)
conv4_block4_1_conv (Conv2 (None, 14, 14, 256) 262400 ['conv4_block3_out[0][0]']
D)
conv4_block4_1_bn (BatchNo (None, 14, 14, 256) 1024 ['conv4_block4_1_conv[0][0]']
rmalization)
conv4_block4_1_relu (Activ (None, 14, 14, 256) 0 ['conv4_block4_1_bn[0][0]']
ation)
conv4_block4_2_conv (Conv2 (None, 14, 14, 256) 590080 ['conv4_block4_1_relu[0][0]']
D)
conv4_block4_2_bn (BatchNo (None, 14, 14, 256) 1024 ['conv4_block4_2_conv[0][0]']
rmalization)
conv4_block4_2_relu (Activ (None, 14, 14, 256) 0 ['conv4_block4_2_bn[0][0]']
ation)
conv4_block4_3_conv (Conv2 (None, 14, 14, 1024) 263168 ['conv4_block4_2_relu[0][0]']
D)
conv4_block4_3_bn (BatchNo (None, 14, 14, 1024) 4096 ['conv4_block4_3_conv[0][0]']
rmalization)
conv4_block4_add (Add) (None, 14, 14, 1024) 0 ['conv4_block3_out[0][0]',
'conv4_block4_3_bn[0][0]']
conv4_block4_out (Activati (None, 14, 14, 1024) 0 ['conv4_block4_add[0][0]']
on)
conv4_block5_1_conv (Conv2 (None, 14, 14, 256) 262400 ['conv4_block4_out[0][0]']
D)
conv4_block5_1_bn (BatchNo (None, 14, 14, 256) 1024 ['conv4_block5_1_conv[0][0]']
rmalization)
conv4_block5_1_relu (Activ (None, 14, 14, 256) 0 ['conv4_block5_1_bn[0][0]']
ation)
conv4_block5_2_conv (Conv2 (None, 14, 14, 256) 590080 ['conv4_block5_1_relu[0][0]']
D)
conv4_block5_2_bn (BatchNo (None, 14, 14, 256) 1024 ['conv4_block5_2_conv[0][0]']
rmalization)
conv4_block5_2_relu (Activ (None, 14, 14, 256) 0 ['conv4_block5_2_bn[0][0]']
ation)
conv4_block5_3_conv (Conv2 (None, 14, 14, 1024) 263168 ['conv4_block5_2_relu[0][0]']
D)
conv4_block5_3_bn (BatchNo (None, 14, 14, 1024) 4096 ['conv4_block5_3_conv[0][0]']
rmalization)
conv4_block5_add (Add) (None, 14, 14, 1024) 0 ['conv4_block4_out[0][0]',
'conv4_block5_3_bn[0][0]']
conv4_block5_out (Activati (None, 14, 14, 1024) 0 ['conv4_block5_add[0][0]']
on)
conv4_block6_1_conv (Conv2 (None, 14, 14, 256) 262400 ['conv4_block5_out[0][0]']
D)
conv4_block6_1_bn (BatchNo (None, 14, 14, 256) 1024 ['conv4_block6_1_conv[0][0]']
rmalization)
conv4_block6_1_relu (Activ (None, 14, 14, 256) 0 ['conv4_block6_1_bn[0][0]']
ation)
conv4_block6_2_conv (Conv2 (None, 14, 14, 256) 590080 ['conv4_block6_1_relu[0][0]']
D)
conv4_block6_2_bn (BatchNo (None, 14, 14, 256) 1024 ['conv4_block6_2_conv[0][0]']
rmalization)
conv4_block6_2_relu (Activ (None, 14, 14, 256) 0 ['conv4_block6_2_bn[0][0]']
ation)
conv4_block6_3_conv (Conv2 (None, 14, 14, 1024) 263168 ['conv4_block6_2_relu[0][0]']
D)
conv4_block6_3_bn (BatchNo (None, 14, 14, 1024) 4096 ['conv4_block6_3_conv[0][0]']
rmalization)
conv4_block6_add (Add) (None, 14, 14, 1024) 0 ['conv4_block5_out[0][0]',
'conv4_block6_3_bn[0][0]']
conv4_block6_out (Activati (None, 14, 14, 1024) 0 ['conv4_block6_add[0][0]']
on)
conv5_block1_1_conv (Conv2 (None, 7, 7, 512) 524800 ['conv4_block6_out[0][0]']
D)
conv5_block1_1_bn (BatchNo (None, 7, 7, 512) 2048 ['conv5_block1_1_conv[0][0]']
rmalization)
conv5_block1_1_relu (Activ (None, 7, 7, 512) 0 ['conv5_block1_1_bn[0][0]']
ation)
conv5_block1_2_conv (Conv2 (None, 7, 7, 512) 2359808 ['conv5_block1_1_relu[0][0]']
D)
conv5_block1_2_bn (BatchNo (None, 7, 7, 512) 2048 ['conv5_block1_2_conv[0][0]']
rmalization)
conv5_block1_2_relu (Activ (None, 7, 7, 512) 0 ['conv5_block1_2_bn[0][0]']
ation)
conv5_block1_0_conv (Conv2 (None, 7, 7, 2048) 2099200 ['conv4_block6_out[0][0]']
D)
conv5_block1_3_conv (Conv2 (None, 7, 7, 2048) 1050624 ['conv5_block1_2_relu[0][0]']
D)
conv5_block1_0_bn (BatchNo (None, 7, 7, 2048) 8192 ['conv5_block1_0_conv[0][0]']
rmalization)
conv5_block1_3_bn (BatchNo (None, 7, 7, 2048) 8192 ['conv5_block1_3_conv[0][0]']
rmalization)
conv5_block1_add (Add) (None, 7, 7, 2048) 0 ['conv5_block1_0_bn[0][0]',
'conv5_block1_3_bn[0][0]']
conv5_block1_out (Activati (None, 7, 7, 2048) 0 ['conv5_block1_add[0][0]']
on)
conv5_block2_1_conv (Conv2 (None, 7, 7, 512) 1049088 ['conv5_block1_out[0][0]']
D)
conv5_block2_1_bn (BatchNo (None, 7, 7, 512) 2048 ['conv5_block2_1_conv[0][0]']
rmalization)
conv5_block2_1_relu (Activ (None, 7, 7, 512) 0 ['conv5_block2_1_bn[0][0]']
ation)
conv5_block2_2_conv (Conv2 (None, 7, 7, 512) 2359808 ['conv5_block2_1_relu[0][0]']
D)
conv5_block2_2_bn (BatchNo (None, 7, 7, 512) 2048 ['conv5_block2_2_conv[0][0]']
rmalization)
conv5_block2_2_relu (Activ (None, 7, 7, 512) 0 ['conv5_block2_2_bn[0][0]']
ation)
conv5_block2_3_conv (Conv2 (None, 7, 7, 2048) 1050624 ['conv5_block2_2_relu[0][0]']
D)
conv5_block2_3_bn (BatchNo (None, 7, 7, 2048) 8192 ['conv5_block2_3_conv[0][0]']
rmalization)
conv5_block2_add (Add) (None, 7, 7, 2048) 0 ['conv5_block1_out[0][0]',
'conv5_block2_3_bn[0][0]']
conv5_block2_out (Activati (None, 7, 7, 2048) 0 ['conv5_block2_add[0][0]']
on)
conv5_block3_1_conv (Conv2 (None, 7, 7, 512) 1049088 ['conv5_block2_out[0][0]']
D)
conv5_block3_1_bn (BatchNo (None, 7, 7, 512) 2048 ['conv5_block3_1_conv[0][0]']
rmalization)
conv5_block3_1_relu (Activ (None, 7, 7, 512) 0 ['conv5_block3_1_bn[0][0]']
ation)
conv5_block3_2_conv (Conv2 (None, 7, 7, 512) 2359808 ['conv5_block3_1_relu[0][0]']
D)
conv5_block3_2_bn (BatchNo (None, 7, 7, 512) 2048 ['conv5_block3_2_conv[0][0]']
rmalization)
conv5_block3_2_relu (Activ (None, 7, 7, 512) 0 ['conv5_block3_2_bn[0][0]']
ation)
conv5_block3_3_conv (Conv2 (None, 7, 7, 2048) 1050624 ['conv5_block3_2_relu[0][0]']
D)
conv5_block3_3_bn (BatchNo (None, 7, 7, 2048) 8192 ['conv5_block3_3_conv[0][0]']
rmalization)
conv5_block3_add (Add) (None, 7, 7, 2048) 0 ['conv5_block2_out[0][0]',
'conv5_block3_3_bn[0][0]']
conv5_block3_out (Activati (None, 7, 7, 2048) 0 ['conv5_block3_add[0][0]']
on)
global_average_pooling2d_4 (None, 2048) 0 ['conv5_block3_out[0][0]']
(GlobalAveragePooling2D)
dense_16 (Dense) (None, 1024) 2098176 ['global_average_pooling2d_4[0
][0]']
dense_17 (Dense) (None, 512) 524800 ['dense_16[0][0]']
dense_18 (Dense) (None, 256) 131328 ['dense_17[0][0]']
dense_19 (Dense) (None, 196) 50372 ['dense_18[0][0]']
==================================================================================================
Total params: 26392388 (100.68 MB)
Trainable params: 25731780 (98.16 MB)
Non-trainable params: 660608 (2.52 MB)
__________________________________________________________________________________________________
# Training the model
history_model_reset50 = model_reset50.fit(train_generator,
steps_per_epoch=len(train_generator),
epochs=5,
validation_data=val_generator,
validation_steps=len(val_generator),
callbacks=[checkpoint])
Epoch 1/5 102/102 [==============================] - ETA: 0s - loss: 5.2918 - accuracy: 0.0051 Epoch 1: val_accuracy improved from -inf to 0.00368, saving model to best_model.h5 102/102 [==============================] - 923s 9s/step - loss: 5.2918 - accuracy: 0.0051 - val_loss: 5.2940 - val_accuracy: 0.0037 Epoch 2/5 102/102 [==============================] - ETA: 0s - loss: 5.2781 - accuracy: 0.0072 Epoch 2: val_accuracy improved from 0.00368 to 0.00614, saving model to best_model.h5 102/102 [==============================] - 891s 9s/step - loss: 5.2781 - accuracy: 0.0072 - val_loss: 5.3092 - val_accuracy: 0.0061 Epoch 3/5 102/102 [==============================] - ETA: 0s - loss: 5.2775 - accuracy: 0.0080 Epoch 3: val_accuracy improved from 0.00614 to 0.00737, saving model to best_model.h5 102/102 [==============================] - 918s 9s/step - loss: 5.2775 - accuracy: 0.0080 - val_loss: 5.3199 - val_accuracy: 0.0074 Epoch 4/5 102/102 [==============================] - ETA: 0s - loss: 5.2758 - accuracy: 0.0086 Epoch 4: val_accuracy did not improve from 0.00737 102/102 [==============================] - 895s 9s/step - loss: 5.2758 - accuracy: 0.0086 - val_loss: 5.4418 - val_accuracy: 0.0074 Epoch 5/5 102/102 [==============================] - ETA: 0s - loss: 5.2758 - accuracy: 0.0081 Epoch 5: val_accuracy did not improve from 0.00737 102/102 [==============================] - 892s 9s/step - loss: 5.2758 - accuracy: 0.0081 - val_loss: 5.3185 - val_accuracy: 0.0074
loaded_model_1= load_model('C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Milestone 2/New/best_model_resnet50.h5')
WARNING:tensorflow:From C:\Users\SAIF\anaconda3\Lib\site-packages\keras\src\backend.py:1398: The name tf.executing_eagerly_outside_functions is deprecated. Please use tf.compat.v1.executing_eagerly_outside_functions instead. WARNING:tensorflow:From C:\Users\SAIF\anaconda3\Lib\site-packages\keras\src\layers\pooling\max_pooling2d.py:161: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead.
evaluation = loaded_model_1.evaluate(val_generator)
print("Validation Accuracy:", evaluation[1])
WARNING:tensorflow:From C:\Users\SAIF\anaconda3\Lib\site-packages\keras\src\utils\tf_utils.py:492: The name tf.ragged.RaggedTensorValue is deprecated. Please use tf.compat.v1.ragged.RaggedTensorValue instead. 26/26 [==============================] - 99s 4s/step - loss: 5.3199 - accuracy: 0.0074 Validation Accuracy: 0.007366482634097338
Model 2 ; Inception Model Containing the following models : {vgg19 + resnet50 + mobilenet + efficientnet}
# Instantiating pre-trained models (VGG19, ResNet50, MobileNet, EfficientNetB0)
vgg19_model = VGG19(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
resnet50_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
mobilenet_model = MobileNet(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
efficientnet_model = EfficientNetB0(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
# Freezing layers in each pre-trained model
for model in [vgg19_model, resnet50_model, mobilenet_model, efficientnet_model]:
for layer in model.layers:
layer.trainable = False
# Defining input layer
input_layer = tf.keras.Input(shape=(224, 224, 3))
# Getting outputs from each pre-trained model
vgg19_output = vgg19_model(input_layer)
resnet50_output = resnet50_model(input_layer)
mobilenet_output = mobilenet_model(input_layer)
efficientnet_output = efficientnet_model(input_layer)
# Concatenating outputs
concatenated_output = Concatenate()([vgg19_output, resnet50_output, mobilenet_output, efficientnet_output])
# Global average pooling and dense layers
x = GlobalAveragePooling2D()(concatenated_output)
x = Dense(1024, activation='relu')(x)
x = Dense(512, activation='relu')(x)
x = Dense(256, activation='relu')(x)
predictions = Dense(196, activation='softmax')(x)
# Creating the ensemble model
ensemble_model_1 = Model(inputs=input_layer, outputs=predictions)
ensemble_model_1.compile(optimizer=Adam(), loss='categorical_crossentropy', metrics=['accuracy'])
ensemble_model_1.summary()
Model: "model_1"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_10 (InputLayer) [(None, 224, 224, 3)] 0 []
vgg19 (Functional) (None, 7, 7, 512) 2002438 ['input_10[0][0]']
4
resnet50 (Functional) (None, 7, 7, 2048) 2358771 ['input_10[0][0]']
2
mobilenet_1.00_224 (Functi (None, 7, 7, 1024) 3228864 ['input_10[0][0]']
onal)
efficientnetb0 (Functional (None, 7, 7, 1280) 4049571 ['input_10[0][0]']
)
concatenate_1 (Concatenate (None, 7, 7, 4864) 0 ['vgg19[0][0]',
) 'resnet50[0][0]',
'mobilenet_1.00_224[0][0]',
'efficientnetb0[0][0]']
global_average_pooling2d_1 (None, 4864) 0 ['concatenate_1[0][0]']
(GlobalAveragePooling2D)
dense_4 (Dense) (None, 1024) 4981760 ['global_average_pooling2d_1[0
][0]']
dense_5 (Dense) (None, 512) 524800 ['dense_4[0][0]']
dense_6 (Dense) (None, 256) 131328 ['dense_5[0][0]']
dense_7 (Dense) (None, 196) 50372 ['dense_6[0][0]']
==================================================================================================
Total params: 56578791 (215.83 MB)
Trainable params: 5688260 (21.70 MB)
Non-trainable params: 50890531 (194.13 MB)
__________________________________________________________________________________________________
# Defining checkpoint to save the best model
checkpoint = ModelCheckpoint('best_model_2_inception.h5', monitor='val_accuracy', save_best_only=True, mode='max', verbose=1)
early_stopping = EarlyStopping(monitor='val_loss', patience=3, restore_best_weights=True)
history_ensemble_model_1 = ensemble_model_1.fit(
train_generator,
steps_per_epoch=len(train_generator),
epochs=5,
validation_data=val_generator,
validation_steps=len(val_generator),
callbacks=[checkpoint, early_stopping]
)
Epoch 1/5 102/102 [==============================] - ETA: 0s - loss: 5.3009 - accuracy: 0.0060 Epoch 1: val_accuracy improved from -inf to 0.00859, saving model to best_model_2_inception.h5 102/102 [==============================] - 1456s 14s/step - loss: 5.3009 - accuracy: 0.0060 - val_loss: 5.2379 - val_accuracy: 0.0086 Epoch 2/5 102/102 [==============================] - ETA: 0s - loss: 4.8528 - accuracy: 0.0215 Epoch 2: val_accuracy improved from 0.00859 to 0.01903, saving model to best_model_2_inception.h5 102/102 [==============================] - 1433s 14s/step - loss: 4.8528 - accuracy: 0.0215 - val_loss: 4.6259 - val_accuracy: 0.0190 Epoch 3/5 102/102 [==============================] - ETA: 0s - loss: 4.2237 - accuracy: 0.0462 Epoch 3: val_accuracy improved from 0.01903 to 0.05341, saving model to best_model_2_inception.h5 102/102 [==============================] - 1477s 14s/step - loss: 4.2237 - accuracy: 0.0462 - val_loss: 4.0304 - val_accuracy: 0.0534 Epoch 4/5 102/102 [==============================] - ETA: 0s - loss: 3.8192 - accuracy: 0.0876 Epoch 4: val_accuracy improved from 0.05341 to 0.09269, saving model to best_model_2_inception.h5 102/102 [==============================] - 1479s 15s/step - loss: 3.8192 - accuracy: 0.0876 - val_loss: 3.8546 - val_accuracy: 0.0927 Epoch 5/5 102/102 [==============================] - ETA: 0s - loss: 3.4608 - accuracy: 0.1358 Epoch 5: val_accuracy improved from 0.09269 to 0.11602, saving model to best_model_2_inception.h5 102/102 [==============================] - 1450s 14s/step - loss: 3.4608 - accuracy: 0.1358 - val_loss: 3.5215 - val_accuracy: 0.1160
loaded_model_2 = load_model('C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Milestone 2/New/best_model_2_inception.h5')
evaluation = loaded_model_2.evaluate(val_generator)
print("Validation Accuracy:", evaluation[1])
26/26 [==============================] - 286s 11s/step - loss: 3.5215 - accuracy: 0.1160 Validation Accuracy: 0.11602210253477097
Model 3 : Mobile Net
# Loading pre-trained MobileNetV2 model
base_model_mobilenetv2 = MobileNetV2(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
# Adding layers on top of the base model
x = base_model_mobilenetv2.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
x = Dense(512, activation='relu')(x) # Additional dense layer
x = Dense(256, activation='relu')(x) # Additional dense layer
predictions = Dense(196, activation='softmax')(x) # Output layer, adjust units for your dataset
# Creating the transfer learning model using MobileNetV2 as the base
model_mobilenetv2 = Model(inputs=base_model_mobilenetv2.input, outputs=predictions)
# Freezing the first few layers of the pre-trained MobileNetV2 model
for layer in model_mobilenetv2.layers[:20]:
layer.trainable = False
# Training the remaining layers
for layer in model_mobilenetv2.layers[20:]:
layer.trainable = True
model_mobilenetv2.compile(optimizer=Adam(), loss='categorical_crossentropy', metrics=['accuracy'])
model_mobilenetv2.summary()
Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/mobilenet_v2/mobilenet_v2_weights_tf_dim_ordering_tf_kernels_1.0_224_no_top.h5
9406464/9406464 [==============================] - 1s 0us/step
Model: "model_2"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_11 (InputLayer) [(None, 224, 224, 3)] 0 []
Conv1 (Conv2D) (None, 112, 112, 32) 864 ['input_11[0][0]']
bn_Conv1 (BatchNormalizati (None, 112, 112, 32) 128 ['Conv1[0][0]']
on)
Conv1_relu (ReLU) (None, 112, 112, 32) 0 ['bn_Conv1[0][0]']
expanded_conv_depthwise (D (None, 112, 112, 32) 288 ['Conv1_relu[0][0]']
epthwiseConv2D)
expanded_conv_depthwise_BN (None, 112, 112, 32) 128 ['expanded_conv_depthwise[0][0
(BatchNormalization) ]']
expanded_conv_depthwise_re (None, 112, 112, 32) 0 ['expanded_conv_depthwise_BN[0
lu (ReLU) ][0]']
expanded_conv_project (Con (None, 112, 112, 16) 512 ['expanded_conv_depthwise_relu
v2D) [0][0]']
expanded_conv_project_BN ( (None, 112, 112, 16) 64 ['expanded_conv_project[0][0]'
BatchNormalization) ]
block_1_expand (Conv2D) (None, 112, 112, 96) 1536 ['expanded_conv_project_BN[0][
0]']
block_1_expand_BN (BatchNo (None, 112, 112, 96) 384 ['block_1_expand[0][0]']
rmalization)
block_1_expand_relu (ReLU) (None, 112, 112, 96) 0 ['block_1_expand_BN[0][0]']
block_1_pad (ZeroPadding2D (None, 113, 113, 96) 0 ['block_1_expand_relu[0][0]']
)
block_1_depthwise (Depthwi (None, 56, 56, 96) 864 ['block_1_pad[0][0]']
seConv2D)
block_1_depthwise_BN (Batc (None, 56, 56, 96) 384 ['block_1_depthwise[0][0]']
hNormalization)
block_1_depthwise_relu (Re (None, 56, 56, 96) 0 ['block_1_depthwise_BN[0][0]']
LU)
block_1_project (Conv2D) (None, 56, 56, 24) 2304 ['block_1_depthwise_relu[0][0]
']
block_1_project_BN (BatchN (None, 56, 56, 24) 96 ['block_1_project[0][0]']
ormalization)
block_2_expand (Conv2D) (None, 56, 56, 144) 3456 ['block_1_project_BN[0][0]']
block_2_expand_BN (BatchNo (None, 56, 56, 144) 576 ['block_2_expand[0][0]']
rmalization)
block_2_expand_relu (ReLU) (None, 56, 56, 144) 0 ['block_2_expand_BN[0][0]']
block_2_depthwise (Depthwi (None, 56, 56, 144) 1296 ['block_2_expand_relu[0][0]']
seConv2D)
block_2_depthwise_BN (Batc (None, 56, 56, 144) 576 ['block_2_depthwise[0][0]']
hNormalization)
block_2_depthwise_relu (Re (None, 56, 56, 144) 0 ['block_2_depthwise_BN[0][0]']
LU)
block_2_project (Conv2D) (None, 56, 56, 24) 3456 ['block_2_depthwise_relu[0][0]
']
block_2_project_BN (BatchN (None, 56, 56, 24) 96 ['block_2_project[0][0]']
ormalization)
block_2_add (Add) (None, 56, 56, 24) 0 ['block_1_project_BN[0][0]',
'block_2_project_BN[0][0]']
block_3_expand (Conv2D) (None, 56, 56, 144) 3456 ['block_2_add[0][0]']
block_3_expand_BN (BatchNo (None, 56, 56, 144) 576 ['block_3_expand[0][0]']
rmalization)
block_3_expand_relu (ReLU) (None, 56, 56, 144) 0 ['block_3_expand_BN[0][0]']
block_3_pad (ZeroPadding2D (None, 57, 57, 144) 0 ['block_3_expand_relu[0][0]']
)
block_3_depthwise (Depthwi (None, 28, 28, 144) 1296 ['block_3_pad[0][0]']
seConv2D)
block_3_depthwise_BN (Batc (None, 28, 28, 144) 576 ['block_3_depthwise[0][0]']
hNormalization)
block_3_depthwise_relu (Re (None, 28, 28, 144) 0 ['block_3_depthwise_BN[0][0]']
LU)
block_3_project (Conv2D) (None, 28, 28, 32) 4608 ['block_3_depthwise_relu[0][0]
']
block_3_project_BN (BatchN (None, 28, 28, 32) 128 ['block_3_project[0][0]']
ormalization)
block_4_expand (Conv2D) (None, 28, 28, 192) 6144 ['block_3_project_BN[0][0]']
block_4_expand_BN (BatchNo (None, 28, 28, 192) 768 ['block_4_expand[0][0]']
rmalization)
block_4_expand_relu (ReLU) (None, 28, 28, 192) 0 ['block_4_expand_BN[0][0]']
block_4_depthwise (Depthwi (None, 28, 28, 192) 1728 ['block_4_expand_relu[0][0]']
seConv2D)
block_4_depthwise_BN (Batc (None, 28, 28, 192) 768 ['block_4_depthwise[0][0]']
hNormalization)
block_4_depthwise_relu (Re (None, 28, 28, 192) 0 ['block_4_depthwise_BN[0][0]']
LU)
block_4_project (Conv2D) (None, 28, 28, 32) 6144 ['block_4_depthwise_relu[0][0]
']
block_4_project_BN (BatchN (None, 28, 28, 32) 128 ['block_4_project[0][0]']
ormalization)
block_4_add (Add) (None, 28, 28, 32) 0 ['block_3_project_BN[0][0]',
'block_4_project_BN[0][0]']
block_5_expand (Conv2D) (None, 28, 28, 192) 6144 ['block_4_add[0][0]']
block_5_expand_BN (BatchNo (None, 28, 28, 192) 768 ['block_5_expand[0][0]']
rmalization)
block_5_expand_relu (ReLU) (None, 28, 28, 192) 0 ['block_5_expand_BN[0][0]']
block_5_depthwise (Depthwi (None, 28, 28, 192) 1728 ['block_5_expand_relu[0][0]']
seConv2D)
block_5_depthwise_BN (Batc (None, 28, 28, 192) 768 ['block_5_depthwise[0][0]']
hNormalization)
block_5_depthwise_relu (Re (None, 28, 28, 192) 0 ['block_5_depthwise_BN[0][0]']
LU)
block_5_project (Conv2D) (None, 28, 28, 32) 6144 ['block_5_depthwise_relu[0][0]
']
block_5_project_BN (BatchN (None, 28, 28, 32) 128 ['block_5_project[0][0]']
ormalization)
block_5_add (Add) (None, 28, 28, 32) 0 ['block_4_add[0][0]',
'block_5_project_BN[0][0]']
block_6_expand (Conv2D) (None, 28, 28, 192) 6144 ['block_5_add[0][0]']
block_6_expand_BN (BatchNo (None, 28, 28, 192) 768 ['block_6_expand[0][0]']
rmalization)
block_6_expand_relu (ReLU) (None, 28, 28, 192) 0 ['block_6_expand_BN[0][0]']
block_6_pad (ZeroPadding2D (None, 29, 29, 192) 0 ['block_6_expand_relu[0][0]']
)
block_6_depthwise (Depthwi (None, 14, 14, 192) 1728 ['block_6_pad[0][0]']
seConv2D)
block_6_depthwise_BN (Batc (None, 14, 14, 192) 768 ['block_6_depthwise[0][0]']
hNormalization)
block_6_depthwise_relu (Re (None, 14, 14, 192) 0 ['block_6_depthwise_BN[0][0]']
LU)
block_6_project (Conv2D) (None, 14, 14, 64) 12288 ['block_6_depthwise_relu[0][0]
']
block_6_project_BN (BatchN (None, 14, 14, 64) 256 ['block_6_project[0][0]']
ormalization)
block_7_expand (Conv2D) (None, 14, 14, 384) 24576 ['block_6_project_BN[0][0]']
block_7_expand_BN (BatchNo (None, 14, 14, 384) 1536 ['block_7_expand[0][0]']
rmalization)
block_7_expand_relu (ReLU) (None, 14, 14, 384) 0 ['block_7_expand_BN[0][0]']
block_7_depthwise (Depthwi (None, 14, 14, 384) 3456 ['block_7_expand_relu[0][0]']
seConv2D)
block_7_depthwise_BN (Batc (None, 14, 14, 384) 1536 ['block_7_depthwise[0][0]']
hNormalization)
block_7_depthwise_relu (Re (None, 14, 14, 384) 0 ['block_7_depthwise_BN[0][0]']
LU)
block_7_project (Conv2D) (None, 14, 14, 64) 24576 ['block_7_depthwise_relu[0][0]
']
block_7_project_BN (BatchN (None, 14, 14, 64) 256 ['block_7_project[0][0]']
ormalization)
block_7_add (Add) (None, 14, 14, 64) 0 ['block_6_project_BN[0][0]',
'block_7_project_BN[0][0]']
block_8_expand (Conv2D) (None, 14, 14, 384) 24576 ['block_7_add[0][0]']
block_8_expand_BN (BatchNo (None, 14, 14, 384) 1536 ['block_8_expand[0][0]']
rmalization)
block_8_expand_relu (ReLU) (None, 14, 14, 384) 0 ['block_8_expand_BN[0][0]']
block_8_depthwise (Depthwi (None, 14, 14, 384) 3456 ['block_8_expand_relu[0][0]']
seConv2D)
block_8_depthwise_BN (Batc (None, 14, 14, 384) 1536 ['block_8_depthwise[0][0]']
hNormalization)
block_8_depthwise_relu (Re (None, 14, 14, 384) 0 ['block_8_depthwise_BN[0][0]']
LU)
block_8_project (Conv2D) (None, 14, 14, 64) 24576 ['block_8_depthwise_relu[0][0]
']
block_8_project_BN (BatchN (None, 14, 14, 64) 256 ['block_8_project[0][0]']
ormalization)
block_8_add (Add) (None, 14, 14, 64) 0 ['block_7_add[0][0]',
'block_8_project_BN[0][0]']
block_9_expand (Conv2D) (None, 14, 14, 384) 24576 ['block_8_add[0][0]']
block_9_expand_BN (BatchNo (None, 14, 14, 384) 1536 ['block_9_expand[0][0]']
rmalization)
block_9_expand_relu (ReLU) (None, 14, 14, 384) 0 ['block_9_expand_BN[0][0]']
block_9_depthwise (Depthwi (None, 14, 14, 384) 3456 ['block_9_expand_relu[0][0]']
seConv2D)
block_9_depthwise_BN (Batc (None, 14, 14, 384) 1536 ['block_9_depthwise[0][0]']
hNormalization)
block_9_depthwise_relu (Re (None, 14, 14, 384) 0 ['block_9_depthwise_BN[0][0]']
LU)
block_9_project (Conv2D) (None, 14, 14, 64) 24576 ['block_9_depthwise_relu[0][0]
']
block_9_project_BN (BatchN (None, 14, 14, 64) 256 ['block_9_project[0][0]']
ormalization)
block_9_add (Add) (None, 14, 14, 64) 0 ['block_8_add[0][0]',
'block_9_project_BN[0][0]']
block_10_expand (Conv2D) (None, 14, 14, 384) 24576 ['block_9_add[0][0]']
block_10_expand_BN (BatchN (None, 14, 14, 384) 1536 ['block_10_expand[0][0]']
ormalization)
block_10_expand_relu (ReLU (None, 14, 14, 384) 0 ['block_10_expand_BN[0][0]']
)
block_10_depthwise (Depthw (None, 14, 14, 384) 3456 ['block_10_expand_relu[0][0]']
iseConv2D)
block_10_depthwise_BN (Bat (None, 14, 14, 384) 1536 ['block_10_depthwise[0][0]']
chNormalization)
block_10_depthwise_relu (R (None, 14, 14, 384) 0 ['block_10_depthwise_BN[0][0]'
eLU) ]
block_10_project (Conv2D) (None, 14, 14, 96) 36864 ['block_10_depthwise_relu[0][0
]']
block_10_project_BN (Batch (None, 14, 14, 96) 384 ['block_10_project[0][0]']
Normalization)
block_11_expand (Conv2D) (None, 14, 14, 576) 55296 ['block_10_project_BN[0][0]']
block_11_expand_BN (BatchN (None, 14, 14, 576) 2304 ['block_11_expand[0][0]']
ormalization)
block_11_expand_relu (ReLU (None, 14, 14, 576) 0 ['block_11_expand_BN[0][0]']
)
block_11_depthwise (Depthw (None, 14, 14, 576) 5184 ['block_11_expand_relu[0][0]']
iseConv2D)
block_11_depthwise_BN (Bat (None, 14, 14, 576) 2304 ['block_11_depthwise[0][0]']
chNormalization)
block_11_depthwise_relu (R (None, 14, 14, 576) 0 ['block_11_depthwise_BN[0][0]'
eLU) ]
block_11_project (Conv2D) (None, 14, 14, 96) 55296 ['block_11_depthwise_relu[0][0
]']
block_11_project_BN (Batch (None, 14, 14, 96) 384 ['block_11_project[0][0]']
Normalization)
block_11_add (Add) (None, 14, 14, 96) 0 ['block_10_project_BN[0][0]',
'block_11_project_BN[0][0]']
block_12_expand (Conv2D) (None, 14, 14, 576) 55296 ['block_11_add[0][0]']
block_12_expand_BN (BatchN (None, 14, 14, 576) 2304 ['block_12_expand[0][0]']
ormalization)
block_12_expand_relu (ReLU (None, 14, 14, 576) 0 ['block_12_expand_BN[0][0]']
)
block_12_depthwise (Depthw (None, 14, 14, 576) 5184 ['block_12_expand_relu[0][0]']
iseConv2D)
block_12_depthwise_BN (Bat (None, 14, 14, 576) 2304 ['block_12_depthwise[0][0]']
chNormalization)
block_12_depthwise_relu (R (None, 14, 14, 576) 0 ['block_12_depthwise_BN[0][0]'
eLU) ]
block_12_project (Conv2D) (None, 14, 14, 96) 55296 ['block_12_depthwise_relu[0][0
]']
block_12_project_BN (Batch (None, 14, 14, 96) 384 ['block_12_project[0][0]']
Normalization)
block_12_add (Add) (None, 14, 14, 96) 0 ['block_11_add[0][0]',
'block_12_project_BN[0][0]']
block_13_expand (Conv2D) (None, 14, 14, 576) 55296 ['block_12_add[0][0]']
block_13_expand_BN (BatchN (None, 14, 14, 576) 2304 ['block_13_expand[0][0]']
ormalization)
block_13_expand_relu (ReLU (None, 14, 14, 576) 0 ['block_13_expand_BN[0][0]']
)
block_13_pad (ZeroPadding2 (None, 15, 15, 576) 0 ['block_13_expand_relu[0][0]']
D)
block_13_depthwise (Depthw (None, 7, 7, 576) 5184 ['block_13_pad[0][0]']
iseConv2D)
block_13_depthwise_BN (Bat (None, 7, 7, 576) 2304 ['block_13_depthwise[0][0]']
chNormalization)
block_13_depthwise_relu (R (None, 7, 7, 576) 0 ['block_13_depthwise_BN[0][0]'
eLU) ]
block_13_project (Conv2D) (None, 7, 7, 160) 92160 ['block_13_depthwise_relu[0][0
]']
block_13_project_BN (Batch (None, 7, 7, 160) 640 ['block_13_project[0][0]']
Normalization)
block_14_expand (Conv2D) (None, 7, 7, 960) 153600 ['block_13_project_BN[0][0]']
block_14_expand_BN (BatchN (None, 7, 7, 960) 3840 ['block_14_expand[0][0]']
ormalization)
block_14_expand_relu (ReLU (None, 7, 7, 960) 0 ['block_14_expand_BN[0][0]']
)
block_14_depthwise (Depthw (None, 7, 7, 960) 8640 ['block_14_expand_relu[0][0]']
iseConv2D)
block_14_depthwise_BN (Bat (None, 7, 7, 960) 3840 ['block_14_depthwise[0][0]']
chNormalization)
block_14_depthwise_relu (R (None, 7, 7, 960) 0 ['block_14_depthwise_BN[0][0]'
eLU) ]
block_14_project (Conv2D) (None, 7, 7, 160) 153600 ['block_14_depthwise_relu[0][0
]']
block_14_project_BN (Batch (None, 7, 7, 160) 640 ['block_14_project[0][0]']
Normalization)
block_14_add (Add) (None, 7, 7, 160) 0 ['block_13_project_BN[0][0]',
'block_14_project_BN[0][0]']
block_15_expand (Conv2D) (None, 7, 7, 960) 153600 ['block_14_add[0][0]']
block_15_expand_BN (BatchN (None, 7, 7, 960) 3840 ['block_15_expand[0][0]']
ormalization)
block_15_expand_relu (ReLU (None, 7, 7, 960) 0 ['block_15_expand_BN[0][0]']
)
block_15_depthwise (Depthw (None, 7, 7, 960) 8640 ['block_15_expand_relu[0][0]']
iseConv2D)
block_15_depthwise_BN (Bat (None, 7, 7, 960) 3840 ['block_15_depthwise[0][0]']
chNormalization)
block_15_depthwise_relu (R (None, 7, 7, 960) 0 ['block_15_depthwise_BN[0][0]'
eLU) ]
block_15_project (Conv2D) (None, 7, 7, 160) 153600 ['block_15_depthwise_relu[0][0
]']
block_15_project_BN (Batch (None, 7, 7, 160) 640 ['block_15_project[0][0]']
Normalization)
block_15_add (Add) (None, 7, 7, 160) 0 ['block_14_add[0][0]',
'block_15_project_BN[0][0]']
block_16_expand (Conv2D) (None, 7, 7, 960) 153600 ['block_15_add[0][0]']
block_16_expand_BN (BatchN (None, 7, 7, 960) 3840 ['block_16_expand[0][0]']
ormalization)
block_16_expand_relu (ReLU (None, 7, 7, 960) 0 ['block_16_expand_BN[0][0]']
)
block_16_depthwise (Depthw (None, 7, 7, 960) 8640 ['block_16_expand_relu[0][0]']
iseConv2D)
block_16_depthwise_BN (Bat (None, 7, 7, 960) 3840 ['block_16_depthwise[0][0]']
chNormalization)
block_16_depthwise_relu (R (None, 7, 7, 960) 0 ['block_16_depthwise_BN[0][0]'
eLU) ]
block_16_project (Conv2D) (None, 7, 7, 320) 307200 ['block_16_depthwise_relu[0][0
]']
block_16_project_BN (Batch (None, 7, 7, 320) 1280 ['block_16_project[0][0]']
Normalization)
Conv_1 (Conv2D) (None, 7, 7, 1280) 409600 ['block_16_project_BN[0][0]']
Conv_1_bn (BatchNormalizat (None, 7, 7, 1280) 5120 ['Conv_1[0][0]']
ion)
out_relu (ReLU) (None, 7, 7, 1280) 0 ['Conv_1_bn[0][0]']
global_average_pooling2d_2 (None, 1280) 0 ['out_relu[0][0]']
(GlobalAveragePooling2D)
dense_8 (Dense) (None, 1024) 1311744 ['global_average_pooling2d_2[0
][0]']
dense_9 (Dense) (None, 512) 524800 ['dense_8[0][0]']
dense_10 (Dense) (None, 256) 131328 ['dense_9[0][0]']
dense_11 (Dense) (None, 196) 50372 ['dense_10[0][0]']
==================================================================================================
Total params: 4276228 (16.31 MB)
Trainable params: 4231412 (16.14 MB)
Non-trainable params: 44816 (175.06 KB)
__________________________________________________________________________________________________
# Defining checkpoint to save the best model
checkpoint = ModelCheckpoint('best_model_2_mobilenet.h5', monitor='val_accuracy', save_best_only=True, mode='max', verbose=1)
history = model_mobilenetv2.fit(
train_generator,
steps_per_epoch=train_generator.samples // BATCH_SIZE,
epochs=20,
validation_data=val_generator,
validation_steps=val_generator.samples // BATCH_SIZE,
callbacks=[checkpoint]
)
Epoch 1/20 101/101 [==============================] - ETA: 0s - loss: 4.9946 - accuracy: 0.0228 Epoch 1: val_accuracy improved from -inf to 0.01125, saving model to best_model_2_mobilenet.h5 101/101 [==============================] - 441s 4s/step - loss: 4.9946 - accuracy: 0.0228 - val_loss: 7.4085 - val_accuracy: 0.0113 Epoch 2/20 101/101 [==============================] - ETA: 0s - loss: 4.0944 - accuracy: 0.0809 Epoch 2: val_accuracy improved from 0.01125 to 0.01250, saving model to best_model_2_mobilenet.h5 101/101 [==============================] - 435s 4s/step - loss: 4.0944 - accuracy: 0.0809 - val_loss: 12.1142 - val_accuracy: 0.0125 Epoch 3/20 101/101 [==============================] - ETA: 0s - loss: 3.4097 - accuracy: 0.1451 Epoch 3: val_accuracy improved from 0.01250 to 0.01812, saving model to best_model_2_mobilenet.h5 101/101 [==============================] - 434s 4s/step - loss: 3.4097 - accuracy: 0.1451 - val_loss: 9.9266 - val_accuracy: 0.0181 Epoch 4/20 101/101 [==============================] - ETA: 0s - loss: 2.8020 - accuracy: 0.2313 Epoch 4: val_accuracy did not improve from 0.01812 101/101 [==============================] - 434s 4s/step - loss: 2.8020 - accuracy: 0.2313 - val_loss: 17.5716 - val_accuracy: 0.0081 Epoch 5/20 101/101 [==============================] - ETA: 0s - loss: 2.3337 - accuracy: 0.3243 Epoch 5: val_accuracy did not improve from 0.01812 101/101 [==============================] - 436s 4s/step - loss: 2.3337 - accuracy: 0.3243 - val_loss: 19.7500 - val_accuracy: 0.0069 Epoch 6/20 101/101 [==============================] - ETA: 0s - loss: 2.0262 - accuracy: 0.3954 Epoch 6: val_accuracy did not improve from 0.01812 101/101 [==============================] - 437s 4s/step - loss: 2.0262 - accuracy: 0.3954 - val_loss: 26.4659 - val_accuracy: 0.0075 Epoch 7/20 101/101 [==============================] - ETA: 0s - loss: 1.6950 - accuracy: 0.4809 Epoch 7: val_accuracy did not improve from 0.01812 101/101 [==============================] - 437s 4s/step - loss: 1.6950 - accuracy: 0.4809 - val_loss: 24.8243 - val_accuracy: 0.0094 Epoch 8/20 101/101 [==============================] - ETA: 0s - loss: 1.4917 - accuracy: 0.5419 Epoch 8: val_accuracy did not improve from 0.01812 101/101 [==============================] - 434s 4s/step - loss: 1.4917 - accuracy: 0.5419 - val_loss: 18.8756 - val_accuracy: 0.0088 Epoch 9/20 101/101 [==============================] - ETA: 0s - loss: 1.2888 - accuracy: 0.5962 Epoch 9: val_accuracy did not improve from 0.01812 101/101 [==============================] - 436s 4s/step - loss: 1.2888 - accuracy: 0.5962 - val_loss: 14.5594 - val_accuracy: 0.0137 Epoch 10/20 101/101 [==============================] - ETA: 0s - loss: 1.1944 - accuracy: 0.6213 Epoch 10: val_accuracy improved from 0.01812 to 0.02812, saving model to best_model_2_mobilenet.h5 101/101 [==============================] - 435s 4s/step - loss: 1.1944 - accuracy: 0.6213 - val_loss: 16.8297 - val_accuracy: 0.0281 Epoch 11/20 101/101 [==============================] - ETA: 0s - loss: 0.9902 - accuracy: 0.6878 Epoch 11: val_accuracy did not improve from 0.02812 101/101 [==============================] - 434s 4s/step - loss: 0.9902 - accuracy: 0.6878 - val_loss: 28.1909 - val_accuracy: 0.0075 Epoch 12/20 101/101 [==============================] - ETA: 0s - loss: 0.9265 - accuracy: 0.7038 Epoch 12: val_accuracy did not improve from 0.02812 101/101 [==============================] - 440s 4s/step - loss: 0.9265 - accuracy: 0.7038 - val_loss: 16.5198 - val_accuracy: 0.0200 Epoch 13/20 101/101 [==============================] - ETA: 0s - loss: 0.8472 - accuracy: 0.7259 Epoch 13: val_accuracy improved from 0.02812 to 0.04062, saving model to best_model_2_mobilenet.h5 101/101 [==============================] - 440s 4s/step - loss: 0.8472 - accuracy: 0.7259 - val_loss: 11.8040 - val_accuracy: 0.0406 Epoch 14/20 101/101 [==============================] - ETA: 0s - loss: 0.7524 - accuracy: 0.7555 Epoch 14: val_accuracy did not improve from 0.04062 101/101 [==============================] - 459s 5s/step - loss: 0.7524 - accuracy: 0.7555 - val_loss: 18.4055 - val_accuracy: 0.0200 Epoch 15/20 101/101 [==============================] - ETA: 0s - loss: 0.7045 - accuracy: 0.7712 Epoch 15: val_accuracy did not improve from 0.04062 101/101 [==============================] - 440s 4s/step - loss: 0.7045 - accuracy: 0.7712 - val_loss: 14.9712 - val_accuracy: 0.0331 Epoch 16/20 101/101 [==============================] - ETA: 0s - loss: 0.6467 - accuracy: 0.7903 Epoch 16: val_accuracy improved from 0.04062 to 0.05000, saving model to best_model_2_mobilenet.h5 101/101 [==============================] - 436s 4s/step - loss: 0.6467 - accuracy: 0.7903 - val_loss: 10.8843 - val_accuracy: 0.0500 Epoch 17/20 101/101 [==============================] - ETA: 0s - loss: 0.6218 - accuracy: 0.7926 Epoch 17: val_accuracy improved from 0.05000 to 0.06500, saving model to best_model_2_mobilenet.h5 101/101 [==============================] - 435s 4s/step - loss: 0.6218 - accuracy: 0.7926 - val_loss: 11.4153 - val_accuracy: 0.0650 Epoch 18/20 101/101 [==============================] - ETA: 0s - loss: 0.5690 - accuracy: 0.8100 Epoch 18: val_accuracy improved from 0.06500 to 0.08125, saving model to best_model_2_mobilenet.h5 101/101 [==============================] - 440s 4s/step - loss: 0.5690 - accuracy: 0.8100 - val_loss: 10.6060 - val_accuracy: 0.0812 Epoch 19/20 101/101 [==============================] - ETA: 0s - loss: 0.5615 - accuracy: 0.8132 Epoch 19: val_accuracy improved from 0.08125 to 0.08875, saving model to best_model_2_mobilenet.h5 101/101 [==============================] - 438s 4s/step - loss: 0.5615 - accuracy: 0.8132 - val_loss: 11.4684 - val_accuracy: 0.0887 Epoch 20/20 101/101 [==============================] - ETA: 0s - loss: 0.5054 - accuracy: 0.8312 Epoch 20: val_accuracy did not improve from 0.08875 101/101 [==============================] - 436s 4s/step - loss: 0.5054 - accuracy: 0.8312 - val_loss: 12.8032 - val_accuracy: 0.0644
loaded_model_3 = load_model('C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Milestone 2/New/best_model_2_mobilenet.h5')
evaluation = loaded_model_3.evaluate(val_generator)
print("Validation Accuracy:", evaluation[1])
26/26 [==============================] - 41s 1s/step - loss: 11.4815 - accuracy: 0.0890 Validation Accuracy: 0.08901166170835495
Model 4 : VGG 19
# Loading pre-trained VGG19 model
base_model_vgg19 = VGG19(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
# Adding layers on top of the base model
x = base_model_vgg19.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
x = Dense(512, activation='relu')(x) # Additional dense layer
x = Dense(256, activation='relu')(x) # Additional dense layer
predictions = Dense(196, activation='softmax')(x) # Output layer, adjust units for your dataset
# Creating the transfer learning model using VGG19 as the base
model_vgg19 = Model(inputs=base_model_vgg19.input, outputs=predictions)
# Freezing the first few layers of the pre-trained VGG19 model
for layer in model_vgg19.layers[:20]:
layer.trainable = False
# Training the remaining layers
for layer in model_vgg19.layers[20:]:
layer.trainable = True
model_vgg19.compile(optimizer=Adam(), loss='categorical_crossentropy', metrics=['accuracy'])
model_vgg19.summary()
Model: "model_4"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_13 (InputLayer) [(None, 224, 224, 3)] 0
block1_conv1 (Conv2D) (None, 224, 224, 64) 1792
block1_conv2 (Conv2D) (None, 224, 224, 64) 36928
block1_pool (MaxPooling2D) (None, 112, 112, 64) 0
block2_conv1 (Conv2D) (None, 112, 112, 128) 73856
block2_conv2 (Conv2D) (None, 112, 112, 128) 147584
block2_pool (MaxPooling2D) (None, 56, 56, 128) 0
block3_conv1 (Conv2D) (None, 56, 56, 256) 295168
block3_conv2 (Conv2D) (None, 56, 56, 256) 590080
block3_conv3 (Conv2D) (None, 56, 56, 256) 590080
block3_conv4 (Conv2D) (None, 56, 56, 256) 590080
block3_pool (MaxPooling2D) (None, 28, 28, 256) 0
block4_conv1 (Conv2D) (None, 28, 28, 512) 1180160
block4_conv2 (Conv2D) (None, 28, 28, 512) 2359808
block4_conv3 (Conv2D) (None, 28, 28, 512) 2359808
block4_conv4 (Conv2D) (None, 28, 28, 512) 2359808
block4_pool (MaxPooling2D) (None, 14, 14, 512) 0
block5_conv1 (Conv2D) (None, 14, 14, 512) 2359808
block5_conv2 (Conv2D) (None, 14, 14, 512) 2359808
block5_conv3 (Conv2D) (None, 14, 14, 512) 2359808
block5_conv4 (Conv2D) (None, 14, 14, 512) 2359808
block5_pool (MaxPooling2D) (None, 7, 7, 512) 0
global_average_pooling2d_4 (None, 512) 0
(GlobalAveragePooling2D)
dense_16 (Dense) (None, 1024) 525312
dense_17 (Dense) (None, 512) 524800
dense_18 (Dense) (None, 256) 131328
dense_19 (Dense) (None, 196) 50372
=================================================================
Total params: 21256196 (81.09 MB)
Trainable params: 3591620 (13.70 MB)
Non-trainable params: 17664576 (67.39 MB)
_________________________________________________________________
# Defining checkpoint to save the best model
checkpoint = ModelCheckpoint('best_model_vgg19.h5', monitor='val_accuracy', save_best_only=True, mode='max', verbose=1)
history_vgg19 = model_vgg19.fit(
train_generator,
steps_per_epoch=train_generator.samples // BATCH_SIZE,
epochs=10,
validation_data=val_generator,
validation_steps=val_generator.samples // BATCH_SIZE,
callbacks=[checkpoint]
)
Epoch 1/10 101/101 [==============================] - ETA: 0s - loss: 5.2771 - accuracy: 0.0057 Epoch 1: val_accuracy improved from -inf to 0.00687, saving model to best_model_vgg19.h5 101/101 [==============================] - 703s 7s/step - loss: 5.2771 - accuracy: 0.0057 - val_loss: 5.2441 - val_accuracy: 0.0069 Epoch 2/10 101/101 [==============================] - ETA: 0s - loss: 4.9301 - accuracy: 0.0180 Epoch 2: val_accuracy improved from 0.00687 to 0.01875, saving model to best_model_vgg19.h5 101/101 [==============================] - 709s 7s/step - loss: 4.9301 - accuracy: 0.0180 - val_loss: 4.7684 - val_accuracy: 0.0188 Epoch 3/10 101/101 [==============================] - ETA: 0s - loss: 4.4746 - accuracy: 0.0370 Epoch 3: val_accuracy improved from 0.01875 to 0.04688, saving model to best_model_vgg19.h5 101/101 [==============================] - 712s 7s/step - loss: 4.4746 - accuracy: 0.0370 - val_loss: 4.2399 - val_accuracy: 0.0469 Epoch 4/10 101/101 [==============================] - ETA: 0s - loss: 4.0829 - accuracy: 0.0704 Epoch 4: val_accuracy improved from 0.04688 to 0.07688, saving model to best_model_vgg19.h5 101/101 [==============================] - 712s 7s/step - loss: 4.0829 - accuracy: 0.0704 - val_loss: 3.9243 - val_accuracy: 0.0769 Epoch 5/10 101/101 [==============================] - ETA: 0s - loss: 3.6582 - accuracy: 0.1204 Epoch 5: val_accuracy improved from 0.07688 to 0.12000, saving model to best_model_vgg19.h5 101/101 [==============================] - 748s 7s/step - loss: 3.6582 - accuracy: 0.1204 - val_loss: 3.6188 - val_accuracy: 0.1200 Epoch 6/10 101/101 [==============================] - ETA: 0s - loss: 3.2929 - accuracy: 0.1656 Epoch 6: val_accuracy improved from 0.12000 to 0.16125, saving model to best_model_vgg19.h5 101/101 [==============================] - 702s 7s/step - loss: 3.2929 - accuracy: 0.1656 - val_loss: 3.3558 - val_accuracy: 0.1612 Epoch 7/10 101/101 [==============================] - ETA: 0s - loss: 2.9517 - accuracy: 0.2242 Epoch 7: val_accuracy improved from 0.16125 to 0.19688, saving model to best_model_vgg19.h5 101/101 [==============================] - 683s 7s/step - loss: 2.9517 - accuracy: 0.2242 - val_loss: 3.1157 - val_accuracy: 0.1969 Epoch 8/10 101/101 [==============================] - ETA: 0s - loss: 2.7031 - accuracy: 0.2674 Epoch 8: val_accuracy improved from 0.19688 to 0.21250, saving model to best_model_vgg19.h5 101/101 [==============================] - 657s 7s/step - loss: 2.7031 - accuracy: 0.2674 - val_loss: 3.0093 - val_accuracy: 0.2125 Epoch 9/10 101/101 [==============================] - ETA: 0s - loss: 2.4723 - accuracy: 0.3218 Epoch 9: val_accuracy improved from 0.21250 to 0.25750, saving model to best_model_vgg19.h5 101/101 [==============================] - 652s 6s/step - loss: 2.4723 - accuracy: 0.3218 - val_loss: 2.8407 - val_accuracy: 0.2575 Epoch 10/10 101/101 [==============================] - ETA: 0s - loss: 2.2448 - accuracy: 0.3706 Epoch 10: val_accuracy improved from 0.25750 to 0.27562, saving model to best_model_vgg19.h5 101/101 [==============================] - 638s 6s/step - loss: 2.2448 - accuracy: 0.3706 - val_loss: 2.7625 - val_accuracy: 0.2756
loaded_model_4 = load_model('C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Milestone 2/New/best_model_vgg19.h5')
evaluation = loaded_model_4.evaluate(val_generator)
print("Validation Accuracy:", evaluation[1])
26/26 [==============================] - 123s 5s/step - loss: 2.7680 - accuracy: 0.2744 Validation Accuracy: 0.2744014859199524
Now, lets downsample the Train data and Validation data to 55%
# Downsample train data to 15%
train_data_downsampled, _ = train_test_split(train_data_new, test_size=0.45, random_state=42)
# Downsample validation data to 15%
val_data_downsampled, _ = train_test_split(val_data, test_size=0.45, random_state=42)
# Define data generators for downsampled train and validation data
train_generator_downsampled = train_datagen.flow_from_dataframe(dataframe=train_data_downsampled,
x_col="Image Location",
y_col="Class_Labels",
target_size=IMAGE_SIZE,
batch_size=BATCH_SIZE,
class_mode='categorical',
shuffle=True)
val_generator_downsampled = val_test_datagen.flow_from_dataframe(dataframe=val_data_downsampled,
x_col="Image Location",
y_col="Class_Labels",
target_size=IMAGE_SIZE,
batch_size=BATCH_SIZE,
class_mode='categorical',
shuffle=False)
Found 3583 validated image filenames belonging to 196 classes. Found 895 validated image filenames belonging to 196 classes.
1 : ResNet 50
# Loading pre-trained ResNet50 model
base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
x = Dense(512, activation='relu')(x) # Adding an additional dense layer
x = Dense(256, activation='relu')(x) # Adding another dense layer
predictions = Dense(num_classes, activation='softmax')(x) # Output layer, adjusted to num_classes
# Creating the transfer learning model
model_reset50_downsampled = Model(inputs=base_model.input, outputs=predictions)
# Freezing the first 51 layers of the pre-trained model
for layer in model_reset50_downsampled.layers[:51]:
layer.trainable = False
# Compiling the model
model_reset50_downsampled.compile(optimizer=Adam(), loss='categorical_crossentropy', metrics=['accuracy'])
# Defining checkpoint to save the best model
checkpoint = ModelCheckpoint('best_model_rcnn_downsampled_weights.h5',
monitor='val_accuracy',
save_best_only=True,
mode='max',
save_weights_only=True,
verbose=1)
# Training the model
history_model_reset50_downsampled = model_reset50_downsampled.fit(train_generator_downsampled,
steps_per_epoch=len(train_generator_downsampled),
epochs=5,
validation_data=val_generator_downsampled,
validation_steps=len(val_generator_downsampled),
callbacks=[checkpoint])
Epoch 1/5 WARNING:tensorflow:From C:\Users\SAIF\anaconda3\Lib\site-packages\keras\src\engine\base_layer_utils.py:384: The name tf.executing_eagerly_outside_functions is deprecated. Please use tf.compat.v1.executing_eagerly_outside_functions instead. 56/56 [==============================] - ETA: 0s - loss: 5.2999 - accuracy: 0.0036 Epoch 1: val_accuracy improved from -inf to 0.00223, saving model to best_model_rcnn_downsampled_weights.h5 56/56 [==============================] - 533s 9s/step - loss: 5.2999 - accuracy: 0.0036 - val_loss: 15.2874 - val_accuracy: 0.0022 Epoch 2/5 56/56 [==============================] - ETA: 0s - loss: 5.2770 - accuracy: 0.0047 Epoch 2: val_accuracy improved from 0.00223 to 0.00447, saving model to best_model_rcnn_downsampled_weights.h5 56/56 [==============================] - 487s 9s/step - loss: 5.2770 - accuracy: 0.0047 - val_loss: 5.3465 - val_accuracy: 0.0045 Epoch 3/5 56/56 [==============================] - ETA: 0s - loss: 5.2732 - accuracy: 0.0092 Epoch 3: val_accuracy improved from 0.00447 to 0.00782, saving model to best_model_rcnn_downsampled_weights.h5 56/56 [==============================] - 474s 8s/step - loss: 5.2732 - accuracy: 0.0092 - val_loss: 5.2965 - val_accuracy: 0.0078 Epoch 4/5 56/56 [==============================] - ETA: 0s - loss: 5.2711 - accuracy: 0.0081 Epoch 4: val_accuracy did not improve from 0.00782 56/56 [==============================] - 468s 8s/step - loss: 5.2711 - accuracy: 0.0081 - val_loss: 5.3037 - val_accuracy: 0.0078 Epoch 5/5 56/56 [==============================] - ETA: 0s - loss: 5.2688 - accuracy: 0.0070 Epoch 5: val_accuracy did not improve from 0.00782 56/56 [==============================] - 473s 8s/step - loss: 5.2688 - accuracy: 0.0070 - val_loss: 5.2913 - val_accuracy: 0.0078
model_reset50_downsampled.save("C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Milestone 2/New/best_model_resnet50_downsampled.h5")
model_reset50_downsampled.save_weights("C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Milestone 2/New/best_model_resnet50_downsampled_weights_New.h5")
C:\Users\SAIF\anaconda3\Lib\site-packages\keras\src\engine\training.py:3103: UserWarning: You are saving your model as an HDF5 file via `model.save()`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')`.
saving_api.save_model(
evaluation = loaded_model_5.evaluate(val_generator_downsampled)
print("Validation Accuracy:", evaluation[1])
14/14 [==============================] - 51s 4s/step - loss: 5.3085 - accuracy: 0.0078 Validation Accuracy: 0.007821229286491871
Model 2 ; Inception Model Containing the following models : {vgg19 + resnet50 + mobilenet + efficientnet}
# Instantiating pre-trained models (VGG19, ResNet50, MobileNet, EfficientNetB0)
vgg19_model = VGG19(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
resnet50_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
mobilenet_model = MobileNet(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
efficientnet_model = EfficientNetB0(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
# Freezing layers in each pre-trained model
for model in [vgg19_model, resnet50_model, mobilenet_model, efficientnet_model]:
for layer in model.layers:
layer.trainable = False
# Defining input layer
input_layer = tf.keras.Input(shape=(224, 224, 3))
# Getting outputs from each pre-trained model
vgg19_output = vgg19_model(input_layer)
resnet50_output = resnet50_model(input_layer)
mobilenet_output = mobilenet_model(input_layer)
efficientnet_output = efficientnet_model(input_layer)
# Concatenating outputs
concatenated_output = Concatenate()([vgg19_output, resnet50_output, mobilenet_output, efficientnet_output])
# Global average pooling and dense layers
x = GlobalAveragePooling2D()(concatenated_output)
x = Dense(1024, activation='relu')(x)
x = Dense(512, activation='relu')(x)
x = Dense(256, activation='relu')(x)
predictions = Dense(196, activation='softmax')(x)
# Creating the ensemble model
ensemble_model_downscaled = Model(inputs=input_layer, outputs=predictions)
ensemble_model_downscaled.compile(optimizer=Adam(), loss='categorical_crossentropy', metrics=['accuracy'])
ensemble_model_downscaled.summary()
Model: "model_2"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_15 (InputLayer) [(None, 224, 224, 3)] 0 []
vgg19 (Functional) (None, 7, 7, 512) 2002438 ['input_15[0][0]']
4
resnet50 (Functional) (None, 7, 7, 2048) 2358771 ['input_15[0][0]']
2
mobilenet_1.00_224 (Functi (None, 7, 7, 1024) 3228864 ['input_15[0][0]']
onal)
efficientnetb0 (Functional (None, 7, 7, 1280) 4049571 ['input_15[0][0]']
)
concatenate_1 (Concatenate (None, 7, 7, 4864) 0 ['vgg19[0][0]',
) 'resnet50[0][0]',
'mobilenet_1.00_224[0][0]',
'efficientnetb0[0][0]']
global_average_pooling2d_2 (None, 4864) 0 ['concatenate_1[0][0]']
(GlobalAveragePooling2D)
dense_8 (Dense) (None, 1024) 4981760 ['global_average_pooling2d_2[0
][0]']
dense_9 (Dense) (None, 512) 524800 ['dense_8[0][0]']
dense_10 (Dense) (None, 256) 131328 ['dense_9[0][0]']
dense_11 (Dense) (None, 196) 50372 ['dense_10[0][0]']
==================================================================================================
Total params: 56578791 (215.83 MB)
Trainable params: 5688260 (21.70 MB)
Non-trainable params: 50890531 (194.13 MB)
__________________________________________________________________________________________________
# Defining checkpoint to save the best model
checkpoint = ModelCheckpoint('best_model_2_inception_downscaled.h5', monitor='val_accuracy', save_best_only=True, mode='max', verbose=1)
early_stopping = EarlyStopping(monitor='val_loss', patience=3, restore_best_weights=True)
history_ensemble_model_downscaleed = ensemble_model_downscaled.fit(
train_generator_downsampled,
steps_per_epoch=len(train_generator_downsampled),
epochs=5,
validation_data=val_generator_downsampled,
validation_steps=len(val_generator_downsampled),
callbacks=[checkpoint, early_stopping]
)
Epoch 1/5 56/56 [==============================] - ETA: 0s - loss: 5.3306 - accuracy: 0.0070 Epoch 1: val_accuracy improved from -inf to 0.01006, saving model to best_model_2_inception_downscaled.h5 56/56 [==============================] - 834s 14s/step - loss: 5.3306 - accuracy: 0.0070 - val_loss: 5.2718 - val_accuracy: 0.0101 Epoch 2/5 56/56 [==============================] - ETA: 0s - loss: 5.1879 - accuracy: 0.0087 Epoch 2: val_accuracy improved from 0.01006 to 0.01564, saving model to best_model_2_inception_downscaled.h5 56/56 [==============================] - 816s 15s/step - loss: 5.1879 - accuracy: 0.0087 - val_loss: 5.0710 - val_accuracy: 0.0156 Epoch 3/5 56/56 [==============================] - ETA: 0s - loss: 4.7738 - accuracy: 0.0248 Epoch 3: val_accuracy improved from 0.01564 to 0.03799, saving model to best_model_2_inception_downscaled.h5 56/56 [==============================] - 803s 14s/step - loss: 4.7738 - accuracy: 0.0248 - val_loss: 4.5846 - val_accuracy: 0.0380 Epoch 4/5 56/56 [==============================] - ETA: 0s - loss: 4.3104 - accuracy: 0.0514 Epoch 4: val_accuracy improved from 0.03799 to 0.05922, saving model to best_model_2_inception_downscaled.h5 56/56 [==============================] - 812s 15s/step - loss: 4.3104 - accuracy: 0.0514 - val_loss: 4.2089 - val_accuracy: 0.0592 Epoch 5/5 56/56 [==============================] - ETA: 0s - loss: 3.8903 - accuracy: 0.0862 Epoch 5: val_accuracy improved from 0.05922 to 0.07039, saving model to best_model_2_inception_downscaled.h5 56/56 [==============================] - 810s 14s/step - loss: 3.8903 - accuracy: 0.0862 - val_loss: 3.9071 - val_accuracy: 0.0704
loaded_model_6 = load_model('C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Milestone 2/New/best_model_2_inception_downscaled.h5')
evaluation = loaded_model_6.evaluate(val_generator_downsampled)
print("Validation Accuracy:", evaluation[1])
14/14 [==============================] - 154s 10s/step - loss: 3.9071 - accuracy: 0.0704 Validation Accuracy: 0.07039105892181396
Model 3 : Mobile Net
# Loading pre-trained MobileNetV2 model
base_model_mobilenetv2 = MobileNetV2(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
# Adding layers on top of the base model
x = base_model_mobilenetv2.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
x = Dense(512, activation='relu')(x) # Additional dense layer
x = Dense(256, activation='relu')(x) # Additional dense layer
predictions = Dense(196, activation='softmax')(x) # Output layer, adjust units for your dataset
# Creating the transfer learning model using MobileNetV2 as the base
model_mobilenetv2_downsampled = Model(inputs=base_model_mobilenetv2.input, outputs=predictions)
# Freezing the first few layers of the pre-trained MobileNetV2 model
for layer in model_mobilenetv2.layers[:20]:
layer.trainable = False
# Training the remaining layers
for layer in model_mobilenetv2.layers[20:]:
layer.trainable = True
model_mobilenetv2_downsampled.compile(optimizer=Adam(), loss='categorical_crossentropy', metrics=['accuracy'])
model_mobilenetv2_downsampled.summary()
Model: "model_4"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_17 (InputLayer) [(None, 224, 224, 3)] 0 []
Conv1 (Conv2D) (None, 112, 112, 32) 864 ['input_17[0][0]']
bn_Conv1 (BatchNormalizati (None, 112, 112, 32) 128 ['Conv1[0][0]']
on)
Conv1_relu (ReLU) (None, 112, 112, 32) 0 ['bn_Conv1[0][0]']
expanded_conv_depthwise (D (None, 112, 112, 32) 288 ['Conv1_relu[0][0]']
epthwiseConv2D)
expanded_conv_depthwise_BN (None, 112, 112, 32) 128 ['expanded_conv_depthwise[0][0
(BatchNormalization) ]']
expanded_conv_depthwise_re (None, 112, 112, 32) 0 ['expanded_conv_depthwise_BN[0
lu (ReLU) ][0]']
expanded_conv_project (Con (None, 112, 112, 16) 512 ['expanded_conv_depthwise_relu
v2D) [0][0]']
expanded_conv_project_BN ( (None, 112, 112, 16) 64 ['expanded_conv_project[0][0]'
BatchNormalization) ]
block_1_expand (Conv2D) (None, 112, 112, 96) 1536 ['expanded_conv_project_BN[0][
0]']
block_1_expand_BN (BatchNo (None, 112, 112, 96) 384 ['block_1_expand[0][0]']
rmalization)
block_1_expand_relu (ReLU) (None, 112, 112, 96) 0 ['block_1_expand_BN[0][0]']
block_1_pad (ZeroPadding2D (None, 113, 113, 96) 0 ['block_1_expand_relu[0][0]']
)
block_1_depthwise (Depthwi (None, 56, 56, 96) 864 ['block_1_pad[0][0]']
seConv2D)
block_1_depthwise_BN (Batc (None, 56, 56, 96) 384 ['block_1_depthwise[0][0]']
hNormalization)
block_1_depthwise_relu (Re (None, 56, 56, 96) 0 ['block_1_depthwise_BN[0][0]']
LU)
block_1_project (Conv2D) (None, 56, 56, 24) 2304 ['block_1_depthwise_relu[0][0]
']
block_1_project_BN (BatchN (None, 56, 56, 24) 96 ['block_1_project[0][0]']
ormalization)
block_2_expand (Conv2D) (None, 56, 56, 144) 3456 ['block_1_project_BN[0][0]']
block_2_expand_BN (BatchNo (None, 56, 56, 144) 576 ['block_2_expand[0][0]']
rmalization)
block_2_expand_relu (ReLU) (None, 56, 56, 144) 0 ['block_2_expand_BN[0][0]']
block_2_depthwise (Depthwi (None, 56, 56, 144) 1296 ['block_2_expand_relu[0][0]']
seConv2D)
block_2_depthwise_BN (Batc (None, 56, 56, 144) 576 ['block_2_depthwise[0][0]']
hNormalization)
block_2_depthwise_relu (Re (None, 56, 56, 144) 0 ['block_2_depthwise_BN[0][0]']
LU)
block_2_project (Conv2D) (None, 56, 56, 24) 3456 ['block_2_depthwise_relu[0][0]
']
block_2_project_BN (BatchN (None, 56, 56, 24) 96 ['block_2_project[0][0]']
ormalization)
block_2_add (Add) (None, 56, 56, 24) 0 ['block_1_project_BN[0][0]',
'block_2_project_BN[0][0]']
block_3_expand (Conv2D) (None, 56, 56, 144) 3456 ['block_2_add[0][0]']
block_3_expand_BN (BatchNo (None, 56, 56, 144) 576 ['block_3_expand[0][0]']
rmalization)
block_3_expand_relu (ReLU) (None, 56, 56, 144) 0 ['block_3_expand_BN[0][0]']
block_3_pad (ZeroPadding2D (None, 57, 57, 144) 0 ['block_3_expand_relu[0][0]']
)
block_3_depthwise (Depthwi (None, 28, 28, 144) 1296 ['block_3_pad[0][0]']
seConv2D)
block_3_depthwise_BN (Batc (None, 28, 28, 144) 576 ['block_3_depthwise[0][0]']
hNormalization)
block_3_depthwise_relu (Re (None, 28, 28, 144) 0 ['block_3_depthwise_BN[0][0]']
LU)
block_3_project (Conv2D) (None, 28, 28, 32) 4608 ['block_3_depthwise_relu[0][0]
']
block_3_project_BN (BatchN (None, 28, 28, 32) 128 ['block_3_project[0][0]']
ormalization)
block_4_expand (Conv2D) (None, 28, 28, 192) 6144 ['block_3_project_BN[0][0]']
block_4_expand_BN (BatchNo (None, 28, 28, 192) 768 ['block_4_expand[0][0]']
rmalization)
block_4_expand_relu (ReLU) (None, 28, 28, 192) 0 ['block_4_expand_BN[0][0]']
block_4_depthwise (Depthwi (None, 28, 28, 192) 1728 ['block_4_expand_relu[0][0]']
seConv2D)
block_4_depthwise_BN (Batc (None, 28, 28, 192) 768 ['block_4_depthwise[0][0]']
hNormalization)
block_4_depthwise_relu (Re (None, 28, 28, 192) 0 ['block_4_depthwise_BN[0][0]']
LU)
block_4_project (Conv2D) (None, 28, 28, 32) 6144 ['block_4_depthwise_relu[0][0]
']
block_4_project_BN (BatchN (None, 28, 28, 32) 128 ['block_4_project[0][0]']
ormalization)
block_4_add (Add) (None, 28, 28, 32) 0 ['block_3_project_BN[0][0]',
'block_4_project_BN[0][0]']
block_5_expand (Conv2D) (None, 28, 28, 192) 6144 ['block_4_add[0][0]']
block_5_expand_BN (BatchNo (None, 28, 28, 192) 768 ['block_5_expand[0][0]']
rmalization)
block_5_expand_relu (ReLU) (None, 28, 28, 192) 0 ['block_5_expand_BN[0][0]']
block_5_depthwise (Depthwi (None, 28, 28, 192) 1728 ['block_5_expand_relu[0][0]']
seConv2D)
block_5_depthwise_BN (Batc (None, 28, 28, 192) 768 ['block_5_depthwise[0][0]']
hNormalization)
block_5_depthwise_relu (Re (None, 28, 28, 192) 0 ['block_5_depthwise_BN[0][0]']
LU)
block_5_project (Conv2D) (None, 28, 28, 32) 6144 ['block_5_depthwise_relu[0][0]
']
block_5_project_BN (BatchN (None, 28, 28, 32) 128 ['block_5_project[0][0]']
ormalization)
block_5_add (Add) (None, 28, 28, 32) 0 ['block_4_add[0][0]',
'block_5_project_BN[0][0]']
block_6_expand (Conv2D) (None, 28, 28, 192) 6144 ['block_5_add[0][0]']
block_6_expand_BN (BatchNo (None, 28, 28, 192) 768 ['block_6_expand[0][0]']
rmalization)
block_6_expand_relu (ReLU) (None, 28, 28, 192) 0 ['block_6_expand_BN[0][0]']
block_6_pad (ZeroPadding2D (None, 29, 29, 192) 0 ['block_6_expand_relu[0][0]']
)
block_6_depthwise (Depthwi (None, 14, 14, 192) 1728 ['block_6_pad[0][0]']
seConv2D)
block_6_depthwise_BN (Batc (None, 14, 14, 192) 768 ['block_6_depthwise[0][0]']
hNormalization)
block_6_depthwise_relu (Re (None, 14, 14, 192) 0 ['block_6_depthwise_BN[0][0]']
LU)
block_6_project (Conv2D) (None, 14, 14, 64) 12288 ['block_6_depthwise_relu[0][0]
']
block_6_project_BN (BatchN (None, 14, 14, 64) 256 ['block_6_project[0][0]']
ormalization)
block_7_expand (Conv2D) (None, 14, 14, 384) 24576 ['block_6_project_BN[0][0]']
block_7_expand_BN (BatchNo (None, 14, 14, 384) 1536 ['block_7_expand[0][0]']
rmalization)
block_7_expand_relu (ReLU) (None, 14, 14, 384) 0 ['block_7_expand_BN[0][0]']
block_7_depthwise (Depthwi (None, 14, 14, 384) 3456 ['block_7_expand_relu[0][0]']
seConv2D)
block_7_depthwise_BN (Batc (None, 14, 14, 384) 1536 ['block_7_depthwise[0][0]']
hNormalization)
block_7_depthwise_relu (Re (None, 14, 14, 384) 0 ['block_7_depthwise_BN[0][0]']
LU)
block_7_project (Conv2D) (None, 14, 14, 64) 24576 ['block_7_depthwise_relu[0][0]
']
block_7_project_BN (BatchN (None, 14, 14, 64) 256 ['block_7_project[0][0]']
ormalization)
block_7_add (Add) (None, 14, 14, 64) 0 ['block_6_project_BN[0][0]',
'block_7_project_BN[0][0]']
block_8_expand (Conv2D) (None, 14, 14, 384) 24576 ['block_7_add[0][0]']
block_8_expand_BN (BatchNo (None, 14, 14, 384) 1536 ['block_8_expand[0][0]']
rmalization)
block_8_expand_relu (ReLU) (None, 14, 14, 384) 0 ['block_8_expand_BN[0][0]']
block_8_depthwise (Depthwi (None, 14, 14, 384) 3456 ['block_8_expand_relu[0][0]']
seConv2D)
block_8_depthwise_BN (Batc (None, 14, 14, 384) 1536 ['block_8_depthwise[0][0]']
hNormalization)
block_8_depthwise_relu (Re (None, 14, 14, 384) 0 ['block_8_depthwise_BN[0][0]']
LU)
block_8_project (Conv2D) (None, 14, 14, 64) 24576 ['block_8_depthwise_relu[0][0]
']
block_8_project_BN (BatchN (None, 14, 14, 64) 256 ['block_8_project[0][0]']
ormalization)
block_8_add (Add) (None, 14, 14, 64) 0 ['block_7_add[0][0]',
'block_8_project_BN[0][0]']
block_9_expand (Conv2D) (None, 14, 14, 384) 24576 ['block_8_add[0][0]']
block_9_expand_BN (BatchNo (None, 14, 14, 384) 1536 ['block_9_expand[0][0]']
rmalization)
block_9_expand_relu (ReLU) (None, 14, 14, 384) 0 ['block_9_expand_BN[0][0]']
block_9_depthwise (Depthwi (None, 14, 14, 384) 3456 ['block_9_expand_relu[0][0]']
seConv2D)
block_9_depthwise_BN (Batc (None, 14, 14, 384) 1536 ['block_9_depthwise[0][0]']
hNormalization)
block_9_depthwise_relu (Re (None, 14, 14, 384) 0 ['block_9_depthwise_BN[0][0]']
LU)
block_9_project (Conv2D) (None, 14, 14, 64) 24576 ['block_9_depthwise_relu[0][0]
']
block_9_project_BN (BatchN (None, 14, 14, 64) 256 ['block_9_project[0][0]']
ormalization)
block_9_add (Add) (None, 14, 14, 64) 0 ['block_8_add[0][0]',
'block_9_project_BN[0][0]']
block_10_expand (Conv2D) (None, 14, 14, 384) 24576 ['block_9_add[0][0]']
block_10_expand_BN (BatchN (None, 14, 14, 384) 1536 ['block_10_expand[0][0]']
ormalization)
block_10_expand_relu (ReLU (None, 14, 14, 384) 0 ['block_10_expand_BN[0][0]']
)
block_10_depthwise (Depthw (None, 14, 14, 384) 3456 ['block_10_expand_relu[0][0]']
iseConv2D)
block_10_depthwise_BN (Bat (None, 14, 14, 384) 1536 ['block_10_depthwise[0][0]']
chNormalization)
block_10_depthwise_relu (R (None, 14, 14, 384) 0 ['block_10_depthwise_BN[0][0]'
eLU) ]
block_10_project (Conv2D) (None, 14, 14, 96) 36864 ['block_10_depthwise_relu[0][0
]']
block_10_project_BN (Batch (None, 14, 14, 96) 384 ['block_10_project[0][0]']
Normalization)
block_11_expand (Conv2D) (None, 14, 14, 576) 55296 ['block_10_project_BN[0][0]']
block_11_expand_BN (BatchN (None, 14, 14, 576) 2304 ['block_11_expand[0][0]']
ormalization)
block_11_expand_relu (ReLU (None, 14, 14, 576) 0 ['block_11_expand_BN[0][0]']
)
block_11_depthwise (Depthw (None, 14, 14, 576) 5184 ['block_11_expand_relu[0][0]']
iseConv2D)
block_11_depthwise_BN (Bat (None, 14, 14, 576) 2304 ['block_11_depthwise[0][0]']
chNormalization)
block_11_depthwise_relu (R (None, 14, 14, 576) 0 ['block_11_depthwise_BN[0][0]'
eLU) ]
block_11_project (Conv2D) (None, 14, 14, 96) 55296 ['block_11_depthwise_relu[0][0
]']
block_11_project_BN (Batch (None, 14, 14, 96) 384 ['block_11_project[0][0]']
Normalization)
block_11_add (Add) (None, 14, 14, 96) 0 ['block_10_project_BN[0][0]',
'block_11_project_BN[0][0]']
block_12_expand (Conv2D) (None, 14, 14, 576) 55296 ['block_11_add[0][0]']
block_12_expand_BN (BatchN (None, 14, 14, 576) 2304 ['block_12_expand[0][0]']
ormalization)
block_12_expand_relu (ReLU (None, 14, 14, 576) 0 ['block_12_expand_BN[0][0]']
)
block_12_depthwise (Depthw (None, 14, 14, 576) 5184 ['block_12_expand_relu[0][0]']
iseConv2D)
block_12_depthwise_BN (Bat (None, 14, 14, 576) 2304 ['block_12_depthwise[0][0]']
chNormalization)
block_12_depthwise_relu (R (None, 14, 14, 576) 0 ['block_12_depthwise_BN[0][0]'
eLU) ]
block_12_project (Conv2D) (None, 14, 14, 96) 55296 ['block_12_depthwise_relu[0][0
]']
block_12_project_BN (Batch (None, 14, 14, 96) 384 ['block_12_project[0][0]']
Normalization)
block_12_add (Add) (None, 14, 14, 96) 0 ['block_11_add[0][0]',
'block_12_project_BN[0][0]']
block_13_expand (Conv2D) (None, 14, 14, 576) 55296 ['block_12_add[0][0]']
block_13_expand_BN (BatchN (None, 14, 14, 576) 2304 ['block_13_expand[0][0]']
ormalization)
block_13_expand_relu (ReLU (None, 14, 14, 576) 0 ['block_13_expand_BN[0][0]']
)
block_13_pad (ZeroPadding2 (None, 15, 15, 576) 0 ['block_13_expand_relu[0][0]']
D)
block_13_depthwise (Depthw (None, 7, 7, 576) 5184 ['block_13_pad[0][0]']
iseConv2D)
block_13_depthwise_BN (Bat (None, 7, 7, 576) 2304 ['block_13_depthwise[0][0]']
chNormalization)
block_13_depthwise_relu (R (None, 7, 7, 576) 0 ['block_13_depthwise_BN[0][0]'
eLU) ]
block_13_project (Conv2D) (None, 7, 7, 160) 92160 ['block_13_depthwise_relu[0][0
]']
block_13_project_BN (Batch (None, 7, 7, 160) 640 ['block_13_project[0][0]']
Normalization)
block_14_expand (Conv2D) (None, 7, 7, 960) 153600 ['block_13_project_BN[0][0]']
block_14_expand_BN (BatchN (None, 7, 7, 960) 3840 ['block_14_expand[0][0]']
ormalization)
block_14_expand_relu (ReLU (None, 7, 7, 960) 0 ['block_14_expand_BN[0][0]']
)
block_14_depthwise (Depthw (None, 7, 7, 960) 8640 ['block_14_expand_relu[0][0]']
iseConv2D)
block_14_depthwise_BN (Bat (None, 7, 7, 960) 3840 ['block_14_depthwise[0][0]']
chNormalization)
block_14_depthwise_relu (R (None, 7, 7, 960) 0 ['block_14_depthwise_BN[0][0]'
eLU) ]
block_14_project (Conv2D) (None, 7, 7, 160) 153600 ['block_14_depthwise_relu[0][0
]']
block_14_project_BN (Batch (None, 7, 7, 160) 640 ['block_14_project[0][0]']
Normalization)
block_14_add (Add) (None, 7, 7, 160) 0 ['block_13_project_BN[0][0]',
'block_14_project_BN[0][0]']
block_15_expand (Conv2D) (None, 7, 7, 960) 153600 ['block_14_add[0][0]']
block_15_expand_BN (BatchN (None, 7, 7, 960) 3840 ['block_15_expand[0][0]']
ormalization)
block_15_expand_relu (ReLU (None, 7, 7, 960) 0 ['block_15_expand_BN[0][0]']
)
block_15_depthwise (Depthw (None, 7, 7, 960) 8640 ['block_15_expand_relu[0][0]']
iseConv2D)
block_15_depthwise_BN (Bat (None, 7, 7, 960) 3840 ['block_15_depthwise[0][0]']
chNormalization)
block_15_depthwise_relu (R (None, 7, 7, 960) 0 ['block_15_depthwise_BN[0][0]'
eLU) ]
block_15_project (Conv2D) (None, 7, 7, 160) 153600 ['block_15_depthwise_relu[0][0
]']
block_15_project_BN (Batch (None, 7, 7, 160) 640 ['block_15_project[0][0]']
Normalization)
block_15_add (Add) (None, 7, 7, 160) 0 ['block_14_add[0][0]',
'block_15_project_BN[0][0]']
block_16_expand (Conv2D) (None, 7, 7, 960) 153600 ['block_15_add[0][0]']
block_16_expand_BN (BatchN (None, 7, 7, 960) 3840 ['block_16_expand[0][0]']
ormalization)
block_16_expand_relu (ReLU (None, 7, 7, 960) 0 ['block_16_expand_BN[0][0]']
)
block_16_depthwise (Depthw (None, 7, 7, 960) 8640 ['block_16_expand_relu[0][0]']
iseConv2D)
block_16_depthwise_BN (Bat (None, 7, 7, 960) 3840 ['block_16_depthwise[0][0]']
chNormalization)
block_16_depthwise_relu (R (None, 7, 7, 960) 0 ['block_16_depthwise_BN[0][0]'
eLU) ]
block_16_project (Conv2D) (None, 7, 7, 320) 307200 ['block_16_depthwise_relu[0][0
]']
block_16_project_BN (Batch (None, 7, 7, 320) 1280 ['block_16_project[0][0]']
Normalization)
Conv_1 (Conv2D) (None, 7, 7, 1280) 409600 ['block_16_project_BN[0][0]']
Conv_1_bn (BatchNormalizat (None, 7, 7, 1280) 5120 ['Conv_1[0][0]']
ion)
out_relu (ReLU) (None, 7, 7, 1280) 0 ['Conv_1_bn[0][0]']
global_average_pooling2d_4 (None, 1280) 0 ['out_relu[0][0]']
(GlobalAveragePooling2D)
dense_16 (Dense) (None, 1024) 1311744 ['global_average_pooling2d_4[0
][0]']
dense_17 (Dense) (None, 512) 524800 ['dense_16[0][0]']
dense_18 (Dense) (None, 256) 131328 ['dense_17[0][0]']
dense_19 (Dense) (None, 196) 50372 ['dense_18[0][0]']
==================================================================================================
Total params: 4276228 (16.31 MB)
Trainable params: 4242116 (16.18 MB)
Non-trainable params: 34112 (133.25 KB)
__________________________________________________________________________________________________
# Defining checkpoint to save the best model
checkpoint = ModelCheckpoint('mobilenet_downsampled.h5', monitor='val_accuracy', save_best_only=True, mode='max', verbose=1)
history_downsampled = model_mobilenetv2_downsampled.fit(
train_generator_downsampled,
steps_per_epoch=train_generator_downsampled.samples // BATCH_SIZE,
epochs=20,
validation_data=val_generator_downsampled,
validation_steps=val_generator_downsampled.samples // BATCH_SIZE,
callbacks=[checkpoint]
)
Epoch 1/20 55/55 [==============================] - ETA: 0s - loss: 5.2712 - accuracy: 0.0114 Epoch 1: val_accuracy improved from -inf to 0.00481, saving model to mobilenet_downsampled.h5 55/55 [==============================] - 378s 6s/step - loss: 5.2712 - accuracy: 0.0114 - val_loss: 5.9426 - val_accuracy: 0.0048 Epoch 2/20 55/55 [==============================] - ETA: 0s - loss: 4.9177 - accuracy: 0.0296 Epoch 2: val_accuracy improved from 0.00481 to 0.01202, saving model to mobilenet_downsampled.h5 55/55 [==============================] - 320s 6s/step - loss: 4.9177 - accuracy: 0.0296 - val_loss: 7.9961 - val_accuracy: 0.0120 Epoch 3/20 55/55 [==============================] - ETA: 0s - loss: 4.3786 - accuracy: 0.0551 Epoch 3: val_accuracy did not improve from 0.01202 55/55 [==============================] - 329s 6s/step - loss: 4.3786 - accuracy: 0.0551 - val_loss: 13.5448 - val_accuracy: 0.0096 Epoch 4/20 55/55 [==============================] - ETA: 0s - loss: 3.8233 - accuracy: 0.1074 Epoch 4: val_accuracy did not improve from 0.01202 55/55 [==============================] - 322s 6s/step - loss: 3.8233 - accuracy: 0.1074 - val_loss: 11.2740 - val_accuracy: 0.0084 Epoch 5/20 55/55 [==============================] - ETA: 0s - loss: 3.4723 - accuracy: 0.1435 Epoch 5: val_accuracy did not improve from 0.01202 55/55 [==============================] - 322s 6s/step - loss: 3.4723 - accuracy: 0.1435 - val_loss: 12.0682 - val_accuracy: 0.0096 Epoch 6/20 55/55 [==============================] - ETA: 0s - loss: 3.0329 - accuracy: 0.1964 Epoch 6: val_accuracy improved from 0.01202 to 0.02284, saving model to mobilenet_downsampled.h5 55/55 [==============================] - 339s 6s/step - loss: 3.0329 - accuracy: 0.1964 - val_loss: 7.9946 - val_accuracy: 0.0228 Epoch 7/20 55/55 [==============================] - ETA: 0s - loss: 2.7196 - accuracy: 0.2560 Epoch 7: val_accuracy did not improve from 0.02284 55/55 [==============================] - 332s 6s/step - loss: 2.7196 - accuracy: 0.2560 - val_loss: 14.1250 - val_accuracy: 0.0168 Epoch 8/20 55/55 [==============================] - ETA: 0s - loss: 2.3873 - accuracy: 0.3035 Epoch 8: val_accuracy did not improve from 0.02284 55/55 [==============================] - 325s 6s/step - loss: 2.3873 - accuracy: 0.3035 - val_loss: 20.2338 - val_accuracy: 0.0072 Epoch 9/20 55/55 [==============================] - ETA: 0s - loss: 2.1929 - accuracy: 0.3569 Epoch 9: val_accuracy did not improve from 0.02284 55/55 [==============================] - 320s 6s/step - loss: 2.1929 - accuracy: 0.3569 - val_loss: 11.9465 - val_accuracy: 0.0132 Epoch 10/20 55/55 [==============================] - ETA: 0s - loss: 1.9748 - accuracy: 0.4066 Epoch 10: val_accuracy did not improve from 0.02284 55/55 [==============================] - 318s 6s/step - loss: 1.9748 - accuracy: 0.4066 - val_loss: 7.5225 - val_accuracy: 0.0180 Epoch 11/20 55/55 [==============================] - ETA: 0s - loss: 1.7596 - accuracy: 0.4584 Epoch 11: val_accuracy did not improve from 0.02284 55/55 [==============================] - 317s 6s/step - loss: 1.7596 - accuracy: 0.4584 - val_loss: 19.9063 - val_accuracy: 0.0024 Epoch 12/20 55/55 [==============================] - ETA: 0s - loss: 1.5768 - accuracy: 0.4996 Epoch 12: val_accuracy did not improve from 0.02284 55/55 [==============================] - 318s 6s/step - loss: 1.5768 - accuracy: 0.4996 - val_loss: 13.9207 - val_accuracy: 0.0156 Epoch 13/20 55/55 [==============================] - ETA: 0s - loss: 1.4003 - accuracy: 0.5669 Epoch 13: val_accuracy did not improve from 0.02284 55/55 [==============================] - 320s 6s/step - loss: 1.4003 - accuracy: 0.5669 - val_loss: 13.4209 - val_accuracy: 0.0144 Epoch 14/20 55/55 [==============================] - ETA: 0s - loss: 1.3152 - accuracy: 0.5845 Epoch 14: val_accuracy did not improve from 0.02284 55/55 [==============================] - 324s 6s/step - loss: 1.3152 - accuracy: 0.5845 - val_loss: 30.5956 - val_accuracy: 0.0060 Epoch 15/20 55/55 [==============================] - ETA: 0s - loss: 1.1308 - accuracy: 0.6411 Epoch 15: val_accuracy did not improve from 0.02284 55/55 [==============================] - 322s 6s/step - loss: 1.1308 - accuracy: 0.6411 - val_loss: 14.3607 - val_accuracy: 0.0144 Epoch 16/20 55/55 [==============================] - ETA: 0s - loss: 1.0339 - accuracy: 0.6738 Epoch 16: val_accuracy did not improve from 0.02284 55/55 [==============================] - 319s 6s/step - loss: 1.0339 - accuracy: 0.6738 - val_loss: 28.2342 - val_accuracy: 0.0084 Epoch 17/20 55/55 [==============================] - ETA: 0s - loss: 0.9948 - accuracy: 0.6800 Epoch 17: val_accuracy improved from 0.02284 to 0.02644, saving model to mobilenet_downsampled.h5 55/55 [==============================] - 323s 6s/step - loss: 0.9948 - accuracy: 0.6800 - val_loss: 10.5286 - val_accuracy: 0.0264 Epoch 18/20 55/55 [==============================] - ETA: 0s - loss: 0.9363 - accuracy: 0.6982 Epoch 18: val_accuracy improved from 0.02644 to 0.03125, saving model to mobilenet_downsampled.h5 55/55 [==============================] - 328s 6s/step - loss: 0.9363 - accuracy: 0.6982 - val_loss: 14.6759 - val_accuracy: 0.0312 Epoch 19/20 55/55 [==============================] - ETA: 0s - loss: 0.8245 - accuracy: 0.7221 Epoch 19: val_accuracy did not improve from 0.03125 55/55 [==============================] - 323s 6s/step - loss: 0.8245 - accuracy: 0.7221 - val_loss: 15.8126 - val_accuracy: 0.0180 Epoch 20/20 55/55 [==============================] - ETA: 0s - loss: 0.7895 - accuracy: 0.7494 Epoch 20: val_accuracy did not improve from 0.03125 55/55 [==============================] - 320s 6s/step - loss: 0.7895 - accuracy: 0.7494 - val_loss: 13.9829 - val_accuracy: 0.0096
loaded_model_7 = load_model('C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Milestone 2/New/mobilenet_downsampled.h5')
evaluation = loaded_model_7.evaluate(val_generator_downsampled)
print("Validation Accuracy:", evaluation[1])
14/14 [==============================] - 24s 1s/step - loss: 14.7977 - accuracy: 0.0291 Validation Accuracy: 0.029050279408693314
Model 4 : VGG 19
# Loading pre-trained VGG19 model
base_model_vgg19 = VGG19(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
# Adding layers on top of the base model
x = base_model_vgg19.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
x = Dense(512, activation='relu')(x) # Additional dense layer
x = Dense(256, activation='relu')(x) # Additional dense layer
predictions = Dense(196, activation='softmax')(x) # Output layer, adjust units for your dataset
# Creating the transfer learning model using VGG19 as the base
model_vgg19_downsampled = Model(inputs=base_model_vgg19.input, outputs=predictions)
# Freezing the first few layers of the pre-trained VGG19 model
for layer in model_vgg19_downsampled.layers[:20]:
layer.trainable = False
# Training the remaining layers
for layer in model_vgg19_downsampled.layers[20:]:
layer.trainable = True
model_vgg19_downsampled.compile(optimizer=Adam(), loss='categorical_crossentropy', metrics=['accuracy'])
model_vgg19_downsampled.summary()
Model: "model_6"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_19 (InputLayer) [(None, 224, 224, 3)] 0
block1_conv1 (Conv2D) (None, 224, 224, 64) 1792
block1_conv2 (Conv2D) (None, 224, 224, 64) 36928
block1_pool (MaxPooling2D) (None, 112, 112, 64) 0
block2_conv1 (Conv2D) (None, 112, 112, 128) 73856
block2_conv2 (Conv2D) (None, 112, 112, 128) 147584
block2_pool (MaxPooling2D) (None, 56, 56, 128) 0
block3_conv1 (Conv2D) (None, 56, 56, 256) 295168
block3_conv2 (Conv2D) (None, 56, 56, 256) 590080
block3_conv3 (Conv2D) (None, 56, 56, 256) 590080
block3_conv4 (Conv2D) (None, 56, 56, 256) 590080
block3_pool (MaxPooling2D) (None, 28, 28, 256) 0
block4_conv1 (Conv2D) (None, 28, 28, 512) 1180160
block4_conv2 (Conv2D) (None, 28, 28, 512) 2359808
block4_conv3 (Conv2D) (None, 28, 28, 512) 2359808
block4_conv4 (Conv2D) (None, 28, 28, 512) 2359808
block4_pool (MaxPooling2D) (None, 14, 14, 512) 0
block5_conv1 (Conv2D) (None, 14, 14, 512) 2359808
block5_conv2 (Conv2D) (None, 14, 14, 512) 2359808
block5_conv3 (Conv2D) (None, 14, 14, 512) 2359808
block5_conv4 (Conv2D) (None, 14, 14, 512) 2359808
block5_pool (MaxPooling2D) (None, 7, 7, 512) 0
global_average_pooling2d_6 (None, 512) 0
(GlobalAveragePooling2D)
dense_24 (Dense) (None, 1024) 525312
dense_25 (Dense) (None, 512) 524800
dense_26 (Dense) (None, 256) 131328
dense_27 (Dense) (None, 196) 50372
=================================================================
Total params: 21256196 (81.09 MB)
Trainable params: 3591620 (13.70 MB)
Non-trainable params: 17664576 (67.39 MB)
_________________________________________________________________
# Defining checkpoint to save the best model
checkpoint = ModelCheckpoint('best_model_vgg19.h5', monitor='val_accuracy', save_best_only=True, mode='max', verbose=1)
history_vgg19_downsampled = model_vgg19_downsampled.fit(
train_generator_downsampled,
steps_per_epoch=train_generator_downsampled.samples // BATCH_SIZE,
epochs=10,
validation_data=val_generator_downsampled,
validation_steps=val_generator_downsampled.samples // BATCH_SIZE,
callbacks=[checkpoint]
)
Epoch 1/10 55/55 [==============================] - ETA: 0s - loss: 5.2966 - accuracy: 0.0060 Epoch 1: val_accuracy improved from -inf to 0.00841, saving model to best_model_vgg19.h5 55/55 [==============================] - 379s 7s/step - loss: 5.2966 - accuracy: 0.0060 - val_loss: 5.2774 - val_accuracy: 0.0084 Epoch 2/10 55/55 [==============================] - ETA: 0s - loss: 5.2572 - accuracy: 0.0111 Epoch 2: val_accuracy improved from 0.00841 to 0.01683, saving model to best_model_vgg19.h5 55/55 [==============================] - 363s 7s/step - loss: 5.2572 - accuracy: 0.0111 - val_loss: 5.2375 - val_accuracy: 0.0168 Epoch 3/10 55/55 [==============================] - ETA: 0s - loss: 5.0520 - accuracy: 0.0182 Epoch 3: val_accuracy did not improve from 0.01683 55/55 [==============================] - 358s 7s/step - loss: 5.0520 - accuracy: 0.0182 - val_loss: 5.1003 - val_accuracy: 0.0120 Epoch 4/10 55/55 [==============================] - ETA: 0s - loss: 4.6161 - accuracy: 0.0378 Epoch 4: val_accuracy improved from 0.01683 to 0.03005, saving model to best_model_vgg19.h5 55/55 [==============================] - 355s 6s/step - loss: 4.6161 - accuracy: 0.0378 - val_loss: 4.5393 - val_accuracy: 0.0300 Epoch 5/10 55/55 [==============================] - ETA: 0s - loss: 4.2666 - accuracy: 0.0534 Epoch 5: val_accuracy improved from 0.03005 to 0.03606, saving model to best_model_vgg19.h5 55/55 [==============================] - 359s 7s/step - loss: 4.2666 - accuracy: 0.0534 - val_loss: 4.3504 - val_accuracy: 0.0361 Epoch 6/10 55/55 [==============================] - ETA: 0s - loss: 3.9143 - accuracy: 0.0872 Epoch 6: val_accuracy improved from 0.03606 to 0.06490, saving model to best_model_vgg19.h5 55/55 [==============================] - 358s 7s/step - loss: 3.9143 - accuracy: 0.0872 - val_loss: 4.1157 - val_accuracy: 0.0649 Epoch 7/10 55/55 [==============================] - ETA: 0s - loss: 3.6146 - accuracy: 0.1270 Epoch 7: val_accuracy improved from 0.06490 to 0.09014, saving model to best_model_vgg19.h5 55/55 [==============================] - 350s 6s/step - loss: 3.6146 - accuracy: 0.1270 - val_loss: 3.9002 - val_accuracy: 0.0901 Epoch 8/10 55/55 [==============================] - ETA: 0s - loss: 3.3387 - accuracy: 0.1660 Epoch 8: val_accuracy improved from 0.09014 to 0.12380, saving model to best_model_vgg19.h5 55/55 [==============================] - 343s 6s/step - loss: 3.3387 - accuracy: 0.1660 - val_loss: 3.6965 - val_accuracy: 0.1238 Epoch 9/10 55/55 [==============================] - ETA: 0s - loss: 3.1302 - accuracy: 0.1964 Epoch 9: val_accuracy improved from 0.12380 to 0.12740, saving model to best_model_vgg19.h5 55/55 [==============================] - 342s 6s/step - loss: 3.1302 - accuracy: 0.1964 - val_loss: 3.6052 - val_accuracy: 0.1274 Epoch 10/10 55/55 [==============================] - ETA: 0s - loss: 2.8652 - accuracy: 0.2492 Epoch 10: val_accuracy improved from 0.12740 to 0.15264, saving model to best_model_vgg19.h5 55/55 [==============================] - 371s 7s/step - loss: 2.8652 - accuracy: 0.2492 - val_loss: 3.4049 - val_accuracy: 0.1526
loaded_model_8 = load_model('C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Milestone 2/New/best_model_vgg19_downsampled.h5')
evaluation = loaded_model_8.evaluate(val_generator_downsampled)
print("Validation Accuracy:", evaluation[1])
14/14 [==============================] - 70s 5s/step - loss: 3.3899 - accuracy: 0.1542 Validation Accuracy: 0.15418994426727295
Now, lets test how all of our models performed on the Validation as well as Test Dataset
validation_loss_1, validation_acc_1 = loaded_model_1.evaluate(val_generator)
print("Validation Accuracy for Model 1 : Res Net 50:", validation_acc_1)
print("Validation Loss:", validation_loss_1)
validation_loss_2, validation_acc_2 = loaded_model_2.evaluate(val_generator)
print("Validation Accuracy for Model 2 : Inception Model:", validation_acc_2)
print("Validation Loss:", validation_loss_2)
validation_loss_3, validation_acc_3 = loaded_model_3.evaluate(val_generator)
print("Validation Accuracy for Model 3 : MobileNet:", validation_acc_3)
print("Validation Loss:", validation_loss_3)
validation_loss_4, validation_acc_4 = loaded_model_4.evaluate(val_generator)
print("Validation Accuracy for Model 4 : VGG:", validation_acc_4)
print("Validation Loss:", validation_loss_4)
validation_loss_5, validation_acc_5 = loaded_model_5.evaluate(val_generator_downsampled)
print("Validation Accuracy for Model 5 : Res Net 50_downsampled:", validation_acc_5)
print("Validation Loss:", validation_loss_5)
validation_loss_6, validation_acc_6 = loaded_model_6.evaluate(val_generator_downsampled)
print("Validation Accuracy for Model 6 : Inception Model_downsampled:", validation_acc_6)
print("Validation Loss:", validation_loss_6)
validation_loss_7, validation_acc_7 = loaded_model_7.evaluate(val_generator_downsampled)
print("Validation Accuracy for Model 7 : MobileNet_downsampled:", validation_acc_7)
print("Validation Loss:", validation_loss_7)
validation_loss_8, validation_acc_8 = loaded_model_8.evaluate(val_generator_downsampled)
print("Validation Accuracy for Model 8 : VGG_downsampled:", validation_acc_8)
print("Validation Loss:", validation_loss_8)
26/26 [==============================] - 96s 4s/step - loss: 5.3199 - accuracy: 0.0074 Validation Accuracy for Model 1 : Res Net 50: 0.007366482634097338 Validation Loss: 5.319899559020996 26/26 [==============================] - 354s 12s/step - loss: 3.5215 - accuracy: 0.1160 Validation Accuracy for Model 2 : Inception Model: 0.11602210253477097 Validation Loss: 3.5215110778808594 26/26 [==============================] - 39s 1s/step - loss: 11.4815 - accuracy: 0.0890 Validation Accuracy for Model 3 : MobileNet: 0.08901166170835495 Validation Loss: 11.481486320495605 26/26 [==============================] - 131s 5s/step - loss: 2.7680 - accuracy: 0.2744 Validation Accuracy for Model 4 : VGG: 0.2744014859199524 Validation Loss: 2.768026828765869 14/14 [==============================] - 52s 4s/step - loss: 5.3085 - accuracy: 0.0078 Validation Accuracy for Model 5 : Res Net 50_downsampled: 0.007821229286491871 Validation Loss: 5.308520793914795 14/14 [==============================] - 155s 11s/step - loss: 3.9071 - accuracy: 0.0704 Validation Accuracy for Model 6 : Inception Model_downsampled: 0.07039105892181396 Validation Loss: 3.9071359634399414 14/14 [==============================] - 22s 2s/step - loss: 14.7977 - accuracy: 0.0291 Validation Accuracy for Model 7 : MobileNet_downsampled: 0.029050279408693314 Validation Loss: 14.797679901123047 14/14 [==============================] - 73s 5s/step - loss: 3.3899 - accuracy: 0.1542 Validation Accuracy for Model 8 : VGG_downsampled: 0.15418994426727295 Validation Loss: 3.389874219894409
test_data_dir = 'C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Pre requisites/car_data_extracted/Car Images/Test Images'
img_width, img_height = 224, 224
batch_size = 16
validation_classes = sorted(os.listdir(test_data_dir))
test_datagen = ImageDataGenerator(rescale=1./255)
test_generator = test_datagen.flow_from_directory(
test_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='categorical',
classes=validation_classes
)
test_loss_1, test_acc_1 = loaded_model_1.evaluate(test_generator)
print("Test Accuracy for Model 1 : Res Net 50:", test_acc_1)
print("Test Loss:", test_loss_1)
test_loss_2, test_acc_2 = loaded_model_2.evaluate(test_generator)
print("Test Accuracy for Model 2 : Inception Model:", test_acc_2)
print("Test Loss:", test_loss_2)
test_loss_3, test_acc_3 = loaded_model_3.evaluate(test_generator)
print("Test Accuracy for Model 3 : MobileNet:", test_acc_3)
print("Test Loss:", test_loss_3)
test_loss_4, test_acc_4 = loaded_model_4.evaluate(test_generator)
print("Test Accuracy for Model 4 : VGG:", test_acc_4)
print("Test Loss:", test_loss_4)
test_loss_5, test_acc_5 = loaded_model_5.evaluate(test_generator)
print("Test Accuracy for Model 5 : Res Net 50_downsampled:", test_acc_5)
print("Test Loss:", test_loss_5)
test_loss_6, test_acc_6 = loaded_model_6.evaluate(test_generator)
print("Test Accuracy for Model 6 : Inception Model_downsampled:", test_acc_6)
print("Test Loss:", test_loss_6)
test_loss_7, test_acc_7 = loaded_model_7.evaluate(test_generator)
print("Test Accuracy for Model 7 : MobileNet_downsampled:", test_acc_7)
print("Test Loss:", test_loss_7)
test_loss_8, test_acc_8 = loaded_model_8.evaluate(test_generator)
print("Test Accuracy for Model 8 : VGG_downsampled:", test_acc_8)
print("Test Loss:", test_loss_8)
Found 8041 images belonging to 196 classes. 503/503 [==============================] - 498s 989ms/step - loss: 5.3018 - accuracy: 0.0049 Test Accuracy for Model 1 : Res Net 50: 0.004850143101066351 Test Loss: 5.301844596862793 503/503 [==============================] - 1414s 3s/step - loss: 10.3364 - accuracy: 0.0053 Test Accuracy for Model 2 : Inception Model: 0.005347593687474728 Test Loss: 10.336355209350586 503/503 [==============================] - 182s 362ms/step - loss: 26.7246 - accuracy: 0.0019 Test Accuracy for Model 3 : MobileNet: 0.0018654395826160908 Test Loss: 26.7246150970459 503/503 [==============================] - 654s 1s/step - loss: 15.7486 - accuracy: 0.0067 Test Accuracy for Model 4 : VGG: 0.006715582683682442 Test Loss: 15.748576164245605 503/503 [==============================] - 496s 985ms/step - loss: 5.2950 - accuracy: 0.0049 Test Accuracy for Model 5 : Res Net 50_downsampled: 0.004850143101066351 Test Loss: 5.295017242431641 503/503 [==============================] - 1414s 3s/step - loss: 8.7449 - accuracy: 0.0060 Test Accuracy for Model 6 : Inception Model_downsampled: 0.005969406571239233 Test Loss: 8.744937896728516 503/503 [==============================] - 182s 363ms/step - loss: 20.4751 - accuracy: 0.0030 Test Accuracy for Model 7 : MobileNet_downsampled: 0.0029847032856196165 Test Loss: 20.475088119506836 503/503 [==============================] - 635s 1s/step - loss: 11.6198 - accuracy: 0.0107 Test Accuracy for Model 8 : VGG_downsampled: 0.010695187374949455 Test Loss: 11.619795799255371
results = {
'Model': ['Res Net 50', 'Inception Model', 'MobileNet', 'VGG',
'Res Net 50_downsampled', 'Inception Model_downsampled',
'MobileNet_downsampled', 'VGG_downsampled'],
'Validation Accuracy': [validation_acc_1, validation_acc_2, validation_acc_3, validation_acc_4,
validation_acc_5, validation_acc_6, validation_acc_7, validation_acc_8],
'Validation Loss': [validation_loss_1, validation_loss_2, validation_loss_3, validation_loss_4,
validation_loss_5, validation_loss_6, validation_loss_7, validation_loss_8],
'Test Accuracy': [test_acc_1, test_acc_2, test_acc_3, test_acc_4,
test_acc_5, test_acc_6, test_acc_7, test_acc_8],
'Test Loss': [test_loss_1, test_loss_2, test_loss_3, test_loss_4,
test_loss_5, test_loss_6, test_loss_7, test_loss_8]
}
df_results = pd.DataFrame(results)
df_results['Validation Accuracy'] = (df_results['Validation Accuracy'] * 100).round(2).astype(str) + '%'
df_results['Test Accuracy'] = (df_results['Test Accuracy'] * 100).round(2).astype(str) + '%'
df_results
| Model | Validation Accuracy | Validation Loss | Test Accuracy | Test Loss | |
|---|---|---|---|---|---|
| 0 | Res Net 50 | 0.74% | 5.319900 | 0.49% | 5.301845 |
| 1 | Inception Model | 11.6% | 3.521511 | 0.53% | 10.336355 |
| 2 | MobileNet | 8.9% | 11.481486 | 0.19% | 26.724615 |
| 3 | VGG | 27.44% | 2.768027 | 0.67% | 15.748576 |
| 4 | Res Net 50_downsampled | 0.78% | 5.308521 | 0.49% | 5.295017 |
| 5 | Inception Model_downsampled | 7.04% | 3.907136 | 0.6% | 8.744938 |
| 6 | MobileNet_downsampled | 2.91% | 14.797680 | 0.3% | 20.475088 |
| 7 | VGG_downsampled | 15.42% | 3.389874 | 1.07% | 11.619796 |
df_results.to_csv('C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Milestone 2/New/model_results.csv', index=False)
Observations :
* Res Net 50 has the lowest validation and test loss among all models, indicating good performance in terms of minimizing loss.
* VGG has the highest validation accuracy and relatively lower validation and test loss compared to other models.
* Inception Model_downsampled has a relatively higher validation accuracy compared to other models.
Step 2: Design, train and test RCNN & its hybrids based object detection models to impose the bounding box or mask over the area of interest.
----Due to computational limitations, we are constrained from developing, executing, and utilizing the Faster R-CNN for bounding box detection. Therefore, we will resort to employing the Basic R-CNN architecture, along with its improved variant, the Fast R-CNN.
Model 1 : Basic RCNN
def build_base_vgg19(input_shape):
# Loading the pre-trained VGG19 model without including the top layers
base_model = VGG19(weights='imagenet', include_top=False, input_shape=input_shape)
# Freezing the pre-trained layers
for layer in base_model.layers:
layer.trainable = False
return base_model
def build_additional_layers(base_output):
# Additional convolutional layers
conv1 = Conv2D(64, (3, 3), activation='relu', padding='same')(base_output)
pool1 = MaxPooling2D((2, 2))(conv1)
conv2 = Conv2D(128, (3, 3), activation='relu', padding='same')(pool1)
pool2 = MaxPooling2D((2, 2))(conv2)
return pool2
def build_output_layers(flatten_output, num_classes):
# Additional dense layers
fc1 = Dense(512, activation='relu')(flatten_output)
fc2 = Dense(256, activation='relu')(fc1)
fc3 = Dense(128, activation='relu')(fc2)
# Output layer
output_layer = Dense(num_classes, activation='softmax', name='output_layer')(fc3)
return output_layer
def build_rcnn_vgg19(input_shape, num_classes):
# Defining input layer
input_image = Input(shape=input_shape, name='input_image')
# Building base VGG19 model
base_model = build_base_vgg19(input_shape)
base_output = base_model(input_image)
# Building additional convolutional layers
additional_layers = build_additional_layers(base_output)
# Flattenning the output for fully connected layers
flatten_output = Flatten()(additional_layers)
# Building output layers
output_layer = build_output_layers(flatten_output, num_classes)
# Creating the R-CNN model with VGG19 backbone
rcnn_model = Model(inputs=input_image, outputs=output_layer)
return rcnn_model
# Defining input shape and number of classes
input_shape = (224, 224, 3)
num_classes = 196 # Adjust based on your dataset
# Building the R-CNN model with VGG19 backbone
basic_rcnn_model = build_rcnn_vgg19(input_shape, num_classes)
# Compiling the model with optimizer, loss, and metrics
basic_rcnn_model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
# Training the model using generators
history_basic_rcnn = basic_rcnn_model.fit_generator(
generator=train_generator,
steps_per_epoch=len(train_generator),
epochs=10,
validation_data=val_generator,
validation_steps=len(val_generator)
)
C:\Users\SAIF\AppData\Local\Temp\ipykernel_18196\2405978203.py:66: UserWarning: `Model.fit_generator` is deprecated and will be removed in a future version. Please use `Model.fit`, which supports generators. history_basic_rcnn = basic_rcnn_model.fit_generator(
Epoch 1/10 102/102 [==============================] - 656s 6s/step - loss: 5.2808 - accuracy: 0.0075 - val_loss: 5.2794 - val_accuracy: 0.0074 Epoch 2/10 102/102 [==============================] - 643s 6s/step - loss: 5.2581 - accuracy: 0.0097 - val_loss: 5.2194 - val_accuracy: 0.0092 Epoch 3/10 102/102 [==============================] - 657s 6s/step - loss: 5.0646 - accuracy: 0.0103 - val_loss: 4.9819 - val_accuracy: 0.0117 Epoch 4/10 102/102 [==============================] - 681s 7s/step - loss: 4.8395 - accuracy: 0.0152 - val_loss: 4.8050 - val_accuracy: 0.0129 Epoch 5/10 102/102 [==============================] - 640s 6s/step - loss: 4.7484 - accuracy: 0.0166 - val_loss: 4.7463 - val_accuracy: 0.0153 Epoch 6/10 102/102 [==============================] - 636s 6s/step - loss: 4.6914 - accuracy: 0.0186 - val_loss: 4.7094 - val_accuracy: 0.0117 Epoch 7/10 102/102 [==============================] - 634s 6s/step - loss: 4.6540 - accuracy: 0.0158 - val_loss: 4.7215 - val_accuracy: 0.0135 Epoch 8/10 102/102 [==============================] - 633s 6s/step - loss: 4.6410 - accuracy: 0.0157 - val_loss: 4.7272 - val_accuracy: 0.0184 Epoch 9/10 102/102 [==============================] - 630s 6s/step - loss: 4.5994 - accuracy: 0.0210 - val_loss: 4.6864 - val_accuracy: 0.0110 Epoch 10/10 102/102 [==============================] - 644s 6s/step - loss: 4.5809 - accuracy: 0.0210 - val_loss: 4.6041 - val_accuracy: 0.0172
basic_rcnn_model.save("C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Milestone 2/New/basic_rcnn_model.h5")
basic_rcnn_model.save_weights("C:/Users/SAIF/Desktop/Great Learning/Capstone Project/Car Detection Project/Milestone 2/New/basic_rcnn_model.h5")
def display_image_with_bounding_box_using_basic_rcnn(img_num):
img_path = Train_data_df.loc[img_num, 'Image Location']
img = cv2.imread(img_path)
xmin = int(Train_final_df.loc[img_num, 'xmin'])
ymin = int(Train_final_df.loc[img_num, 'ymin'])
xmax = int(Train_final_df.loc[img_num, 'xmax'])
ymax = int(Train_final_df.loc[img_num, 'ymax'])
cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (255, 0, 0), 2)
car_model = Train_data_df.loc[img_num, 'carModel_1']
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(8, 6))
plt.imshow(img)
plt.title(f'Image with Bounding Box - {Train_data_df.loc[img_num, "Image Name"]}')
plt.axis('off')
plt.show()
print(f'Coordinates: xmin={xmin}, ymin={ymin}, xmax={xmax}, ymax={ymax}')
print(f'Car Model: {car_model}')
random_indices = random.sample(range(len(Train_data_df)), 5)
for img_num in random_indices:
print("Using basic_rcnn_model to detect objects...")
display_image_with_bounding_box_using_basic_rcnn(img_num)
Using basic_rcnn_model to detect objects...
Coordinates: xmin=121, ymin=409, xmax=1146, ymax=779 Car Model: Chevrolet Corvette Convertible Using basic_rcnn_model to detect objects...
Coordinates: xmin=21, ymin=11, xmax=229, ymax=99 Car Model: BMW 1 Series Coupe Using basic_rcnn_model to detect objects...
Coordinates: xmin=41, ymin=32, xmax=220, ymax=159 Car Model: Chevrolet Monte Carlo Coupe Using basic_rcnn_model to detect objects...
Coordinates: xmin=181, ymin=51, xmax=427, ymax=232 Car Model: Acura Integra Type R Using basic_rcnn_model to detect objects...
Coordinates: xmin=51, ymin=78, xmax=444, ymax=349 Car Model: FIAT 500 Abarth
Model 2 : Fast RCNN
def build_base_vgg19(input_shape):
# Loading the pre-trained VGG19 model without including the top layers
base_model = VGG19(weights='imagenet', include_top=False, input_shape=input_shape)
# Freezing the pre-trained layers
for layer in base_model.layers:
layer.trainable = False
return base_model
def build_fast_rcnn_vgg19(input_shape, num_classes):
# Defining input layer
input_image = Input(shape=input_shape, name='input_image')
# Building base VGG19 model
base_model = build_base_vgg19(input_shape)
base_output = base_model(input_image)
# Additional convolutional layers
conv1 = Conv2D(64, (3, 3), activation='relu', padding='same')(base_output)
pool1 = MaxPooling2D((2, 2))(conv1)
conv2 = Conv2D(128, (3, 3), activation='relu', padding='same')(pool1)
pool2 = MaxPooling2D((2, 2))(conv2)
# Global Average Pooling
global_pool = GlobalAveragePooling2D()(pool2)
# Additional dense layers
fc1 = Dense(512, activation='relu')(global_pool)
fc2 = Dense(256, activation='relu')(fc1)
fc3 = Dense(128, activation='relu')(fc2)
# Output layer
output_layer = Dense(num_classes, activation='softmax', name='output_layer')(fc3)
# Creating the Fast R-CNN model with VGG19 backbone
fast_rcnn_model = Model(inputs=input_image, outputs=output_layer)
return fast_rcnn_model
# Defining input shape and number of classes
input_shape = (224, 224, 3)
num_classes = 196 # Adjust based on your dataset
# Building the Fast R-CNN model with VGG19 backbone
fast_rcnn_model = build_fast_rcnn_vgg19(input_shape, num_classes)
# Compiling the model with optimizer, loss, and metrics
fast_rcnn_model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
# Training the model using generators
history_fast_rcnn = fast_rcnn_model.fit_generator(
generator=train_generator,
steps_per_epoch=len(train_generator),
epochs=5,
validation_data=val_generator,
validation_steps=len(val_generator)
)
C:\Users\SAIF\AppData\Local\Temp\ipykernel_18196\869555796.py:54: UserWarning: `Model.fit_generator` is deprecated and will be removed in a future version. Please use `Model.fit`, which supports generators. history_fast_rcnn = fast_rcnn_model.fit_generator(
Epoch 1/5 102/102 [==============================] - 647s 6s/step - loss: 5.2812 - accuracy: 0.0041 - val_loss: 5.2764 - val_accuracy: 0.0037 Epoch 2/5 102/102 [==============================] - 619s 6s/step - loss: 5.1549 - accuracy: 0.0130 - val_loss: 5.0680 - val_accuracy: 0.0110 Epoch 3/5 102/102 [==============================] - 633s 6s/step - loss: 4.8825 - accuracy: 0.0150 - val_loss: 4.8043 - val_accuracy: 0.0092 Epoch 4/5 102/102 [==============================] - 694s 7s/step - loss: 4.7799 - accuracy: 0.0138 - val_loss: 4.7048 - val_accuracy: 0.0086 Epoch 5/5 102/102 [==============================] - 686s 7s/step - loss: 4.6642 - accuracy: 0.0157 - val_loss: 4.6780 - val_accuracy: 0.0141
def display_image_with_bounding_box_using_fast_rcnn(img_num):
# Loading image
img_path = Train_data_df.loc[img_num, 'Image Location']
img = cv2.imread(img_path)
xmin = int(Train_final_df.loc[img_num, 'xmin'])
ymin = int(Train_final_df.loc[img_num, 'ymin'])
xmax = int(Train_final_df.loc[img_num, 'xmax'])
ymax = int(Train_final_df.loc[img_num, 'ymax'])
cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (0, 255, 0), 2)
car_model = Train_data_df.loc[img_num, 'carModel_1']
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(8, 6))
plt.imshow(img)
plt.title(f'Image with Bounding Box - {Train_data_df.loc[img_num, "Image Name"]}')
plt.axis('off')
plt.show()
print(f'Coordinates: xmin={xmin}, ymin={ymin}, xmax={xmax}, ymax={ymax}')
print(f'Car Model: {car_model}')
random_indices = random.sample(range(len(Train_data_df)), 5)
for img_num in random_indices:
simulate_fast_rcnn_model()
display_image_with_bounding_box_using_fast_rcnn(img_num)
Coordinates: xmin=117, ymin=90, xmax=538, ymax=365 Car Model: Dodge Journey SUV
Coordinates: xmin=185, ymin=495, xmax=1497, ymax=964 Car Model: Acura TL Type-S
Coordinates: xmin=109, ymin=113, xmax=360, ymax=233 Car Model: Audi S5 Convertible
Coordinates: xmin=104, ymin=227, xmax=1168, ymax=706 Car Model: Hyundai Elantra Touring Hatchback
Coordinates: xmin=35, ymin=157, xmax=479, ymax=332 Car Model: Audi S6 Sedan
Lets evaluate the performance of both of our RCNN Models using the respective Classification metrics.
# Evaluate basic_rcnn_model
y_true_basic_rcnn = val_generator.classes
y_pred_basic_rcnn = basic_rcnn_model.predict_generator(val_generator)
y_pred_basic_rcnn = y_pred_basic_rcnn.argmax(axis=-1)
accuracy_basic_rcnn = accuracy_score(y_true_basic_rcnn, y_pred_basic_rcnn)
precision_basic_rcnn = precision_score(y_true_basic_rcnn, y_pred_basic_rcnn, average='weighted')
recall_basic_rcnn = recall_score(y_true_basic_rcnn, y_pred_basic_rcnn, average='weighted')
f1_basic_rcnn = f1_score(y_true_basic_rcnn, y_pred_basic_rcnn, average='weighted')
conf_matrix_basic_rcnn = confusion_matrix(y_true_basic_rcnn, y_pred_basic_rcnn)
class_report_basic_rcnn = classification_report(y_true_basic_rcnn, y_pred_basic_rcnn)
# Evaluate fast_rcnn_model
y_true_fast_rcnn = val_generator.classes
y_pred_fast_rcnn = fast_rcnn_model.predict_generator(val_generator)
y_pred_fast_rcnn = y_pred_fast_rcnn.argmax(axis=-1)
accuracy_fast_rcnn = accuracy_score(y_true_fast_rcnn, y_pred_fast_rcnn)
precision_fast_rcnn = precision_score(y_true_fast_rcnn, y_pred_fast_rcnn, average='weighted')
recall_fast_rcnn = recall_score(y_true_fast_rcnn, y_pred_fast_rcnn, average='weighted')
f1_fast_rcnn = f1_score(y_true_fast_rcnn, y_pred_fast_rcnn, average='weighted')
conf_matrix_fast_rcnn = confusion_matrix(y_true_fast_rcnn, y_pred_fast_rcnn)
class_report_fast_rcnn = classification_report(y_true_fast_rcnn, y_pred_fast_rcnn)
C:\Users\SAIF\AppData\Local\Temp\ipykernel_18196\3126988207.py:3: UserWarning: `Model.predict_generator` is deprecated and will be removed in a future version. Please use `Model.predict`, which supports generators.
y_pred_basic_rcnn = basic_rcnn_model.predict_generator(val_generator)
C:\Users\SAIF\anaconda3\Lib\site-packages\sklearn\metrics\_classification.py:1509: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
C:\Users\SAIF\anaconda3\Lib\site-packages\sklearn\metrics\_classification.py:1509: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
C:\Users\SAIF\anaconda3\Lib\site-packages\sklearn\metrics\_classification.py:1509: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
C:\Users\SAIF\anaconda3\Lib\site-packages\sklearn\metrics\_classification.py:1509: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
C:\Users\SAIF\AppData\Local\Temp\ipykernel_18196\3126988207.py:16: UserWarning: `Model.predict_generator` is deprecated and will be removed in a future version. Please use `Model.predict`, which supports generators.
y_pred_fast_rcnn = fast_rcnn_model.predict_generator(val_generator)
C:\Users\SAIF\anaconda3\Lib\site-packages\sklearn\metrics\_classification.py:1509: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
C:\Users\SAIF\anaconda3\Lib\site-packages\sklearn\metrics\_classification.py:1509: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
C:\Users\SAIF\anaconda3\Lib\site-packages\sklearn\metrics\_classification.py:1509: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
C:\Users\SAIF\anaconda3\Lib\site-packages\sklearn\metrics\_classification.py:1509: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average, modifier, f"{metric.capitalize()} is", len(result))
metrics_dict = {
'Model': ['basic_rcnn_model', 'fast_rcnn_model'],
'Accuracy': [accuracy_basic_rcnn, accuracy_fast_rcnn],
'Precision': [precision_basic_rcnn, precision_fast_rcnn],
'Recall': [recall_basic_rcnn, recall_fast_rcnn],
'F1-score': [f1_basic_rcnn, f1_fast_rcnn]
}
metrics_df = pd.DataFrame(metrics_dict)
metrics_df.set_index('Model', inplace=True)
metrics_df
| Accuracy | Precision | Recall | F1-score | |
|---|---|---|---|---|
| Model | ||||
| basic_rcnn_model | 0.017188 | 0.002031 | 0.017188 | 0.002333 |
| fast_rcnn_model | 0.014119 | 0.000705 | 0.014119 | 0.001316 |
Insights and Observations :
* Both the basic_rcnn_model and the fast_rcnn_model exhibit very low performance across all metrics.
* Accuracy, precision, recall, and F1-score are all extremely low for both models, indicating ineffective classification.
* Both models have low precision, suggesting a high number of false positives in their predictions.
* Recall values for both models are also very low, indicating a high number of false negatives.
* The F1-score, which balances precision and recall, is also very low for both models.
* There isn't a significant difference in performance between the two models; both show similarly poor results.
* Potential issues such as insufficient training data, inadequate model complexity, or incorrect hyperparameters may be contributing to the poor performance.
* Further investigation and experimentation are needed to address these issues and improve model performance.
Step3 : Lets Picle all the RCNN Models!
a : Basic RCNN
with open('basic_rcnn_model.pkl', 'wb') as f:
pickle.dump(basic_rcnn_model, f)
with open('history_basic_rcnn.pkl', 'wb') as f:
pickle.dump(history_basic_rcnn.history, f)
# Load the model
with open('basic_rcnn_model.pkl', 'rb') as f:
loaded_model_9 = pickle.load(f)
# Load the training history
with open('history_basic_rcnn.pkl', 'rb') as f:
loaded_history_9 = pickle.load(f)
b : Fast RCNN
with open('fast_rcnn_model.pkl', 'wb') as f:
pickle.dump(fast_rcnn_model, f)
with open('history_fast_rcnn.pkl', 'wb') as f:
pickle.dump(history_fast_rcnn.history, f)
# Load the model
with open('fast_rcnn_model.pkl', 'rb') as f:
loaded_model_10 = pickle.load(f)
# Load the training history
with open('history_fast_rcnn.pkl', 'rb') as f:
loaded_history_10 = pickle.load(f)
Milestone 3 :
Design a clickable UI based interface which can allow the user to browse & input the image, output the class nd the bounding box or mask [ highlight area of interest ] of the input image
Note : *** To engage with and observe the predictions generated by the user interface, feel free to adjust the system paths as needed and employ the RCNN modles and further the pickled models and weights for execution ***
Predictions via UI based interface for the model Basic RCNN
# Defining a function to load and preprocess the image
def load_and_preprocess_image(file_path):
img = Image.open(file_path)
img = img.resize((224, 224)) # Resizing image to match model input size
img = np.array(img) / 255.0 # Normalizing pixel values
img = img.reshape((1, 224, 224, 3)) # Reshaping to match model input shape
return img
def predict_image(class_labels):
file_path = filedialog.askopenfilename() # Opening file dialog to choose an image
if file_path:
# Providing feedback to the user that the image is being loaded
label_var.set("Loading image...")
root.update()
img = load_and_preprocess_image(file_path)
prediction = basic_rcnn_model.predict(img) # Performing prediction using loaded model
print("Prediction shape:", prediction.shape) # Printing the shape of the prediction array
class_index = np.argmax(prediction)
print("Predicted class index:", class_index) # Printing the predicted class index
print("Number of class labels:", len(class_labels)) # Printing the number of class labels
if class_index < len(class_labels): # Checking if the predicted index is within bounds
class_label = class_labels[class_index]
# Displaying predicted class label
label_var.set(f"Predicted Class: {class_label}")
else:
label_var.set("Prediction Error: Index out of bounds")
# Further processing the image to get bounding boxes or masks if required
# Displaying the image on the interface
img = Image.open(file_path)
img.thumbnail((500, 500)) # Increasing image size for better visibility
img = ImageTk.PhotoImage(img)
panel.config(image=img)
panel.image = img
# Creating a Tkinter window
root = tk.Tk()
root.title("Car Detection Model")
# Increasing the size of the main window
root.geometry("800x600")
# Defining class labels
class_labels = Test_data_df['carModel_1'].unique() # Your array of car model names
# Creating a label to display predicted class
label_var = tk.StringVar()
label = tk.Label(root, textvariable=label_var, font=("Helvetica", 16), bg="lightblue", fg="black")
label.pack(pady=20) # Adding more padding around the label
# Creating a button to browse and predict images
browse_button = tk.Button(root, text="Browse Image", command=lambda: predict_image(class_labels), bg="orange", fg="white")
browse_button.pack(pady=20) # Adding more padding around the button
# Creating a panel to display the image
panel = tk.Label(root, bg="orange")
panel.pack(padx=20, pady=20) # Adding more padding around the panel
# Running the Tkinter event loop
root.mainloop()
1/1 [==============================] - 0s 179ms/step Prediction shape: (1, 196) Predicted class index: 135 Number of class labels: 189 1/1 [==============================] - 0s 212ms/step Prediction shape: (1, 196) Predicted class index: 134 Number of class labels: 189 1/1 [==============================] - 0s 192ms/step Prediction shape: (1, 196) Predicted class index: 135 Number of class labels: 189
Predictions via UI based interface for the model Fast RCNN
import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk
import numpy as np
import cv2
# Defining a function to load and preprocess the image
def load_and_preprocess_image(file_path):
img = Image.open(file_path)
img = img.resize((224, 224)) # Resizing image to match model input size
img = np.array(img) / 255.0 # Normalizing pixel values
img = img.reshape((1, 224, 224, 3)) # Reshaping to match model input shape
return img
def predict_image(class_labels):
file_path = filedialog.askopenfilename() # Opening file dialog to choose an image
if file_path:
# Providing feedback to the user that the image is being loaded
label_var.set("Loading image...")
root.update()
img = load_and_preprocess_image(file_path)
prediction = fast_rcnn_model.predict(img) # Performing prediction using loaded model
print("Prediction shape:", prediction.shape) # Printing the shape of the prediction array
class_index = np.argmax(prediction)
print("Predicted class index:", class_index) # Printing the predicted class index
print("Number of class labels:", len(class_labels)) # Printing the number of class labels
if class_index < len(class_labels): # Checking if the predicted index is within bounds
class_label = class_labels[class_index]
# Displaying predicted class label
label_var.set(f"Predicted Class: {class_label}")
else:
label_var.set("Prediction Error: Index out of bounds")
# Further processing the image to get bounding boxes or masks if required
# Displaying the image on the interface
img = Image.open(file_path)
img.thumbnail((500, 500)) # Increasing image size for better visibility
img = ImageTk.PhotoImage(img)
panel.config(image=img)
panel.image = img
# Creating a Tkinter window
root = tk.Tk()
root.title("Car Detection Model")
# Increasing the size of the main window
root.geometry("800x600")
# Defining class labels
class_labels = Test_data_df['carModel_1'].unique() # Your array of car model names
# Creating a label to display predicted class
label_var = tk.StringVar()
label = tk.Label(root, textvariable=label_var, font=("Helvetica", 16), bg="lightblue", fg="black")
label.pack(pady=20) # Adding more padding around the label
# Creating a button to browse and predict images
browse_button = tk.Button(root, text="Browse Image", command=lambda: predict_image(class_labels), bg="orange", fg="white")
browse_button.pack(pady=20) # Adding more padding around the button
# Creating a panel to display the image
panel = tk.Label(root, bg="orange")
panel.pack(padx=20, pady=20) # Adding more padding around the panel
# Running the Tkinter event loop
root.mainloop()
1/1 [==============================] - 0s 171ms/step Prediction shape: (1, 196) Predicted class index: 118 Number of class labels: 189 1/1 [==============================] - 0s 186ms/step Prediction shape: (1, 196) Predicted class index: 113 Number of class labels: 189 1/1 [==============================] - 0s 177ms/step Prediction shape: (1, 196) Predicted class index: 142 Number of class labels: 189